面向对象的编程求助课后作业求助
class ticket():def __init__(self,weekend=False,child=False):
self.qian=100
if weekend:
self.inc=1.2
else:
self.inc=1
if child:
self.discount=0.5
else:
self.discount=1
def price(self,num):
return self.qian*self.inc*self.discount*num
>>>child=ticket(child=True)
>>> adult=ticket()
print(adult.price(2)+child.price(1))
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
print(adult.price(2)+child.price(1))
AttributeError: 'Ticket' object has no attribute 'price'
课后作业为啥会一直报错呀 缩进问题,price函数定义要和__init__同级:class ticket():
def __init__(self, weekend=False, child=False):
self.qian = 100
if weekend:
self.inc = 1.2
else:
self.inc = 1
if child:
self.discount = 0.5
else:
self.discount = 1
def price(self, num):
return self.qian * self.inc * self.discount * num
页:
[1]