|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class Ticket():
- def __int__(self,weekend =False,child = False):
- self.exp =100
- if weekend:
- self.inc =1.2
- else:
- self.inc =1
- if child:
- self.discount =0.5
- else:
- self.discount =1
- def calcPrice(self,num):
- return self.exp*self.inc*self.discount*num
-
复制代码
输入child =Ticket(child =True)后出现如下问题,请问是哪里不对啊?
>>> child =Ticket(child =True)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
child =Ticket(child =True)
TypeError: object() takes no parameters
本帖最后由 thexiosi 于 2018-4-14 17:05 编辑
hi 问题已经找到,共两处:(1)魔化方法__init__ 名称错误,应该为 __init__ ,文中为__int__ (2)代码缩进ms也存在问题,请调整 - class Ticket():
- def __init__(self,weekend =False,child = False):
- self.exp =100
- if weekend:
- self.inc =1.2
- else:
- self.inc =1
- if child:
- self.discount =0.5
- else:
- self.discount =1
- def calcPrice(self,num):
- return self.exp*self.inc*self.discount*num
复制代码
>>> kid = Ticket(child = True)
>>> kid.calcPrice(2)
100.0
>>>
|
|