【发布时间】:2017-11-12 22:56:05
【问题描述】:
这是我的代码: 我不能在设置任务时更改任何内容,但我找不到我的错误?感谢所有和任何帮助!
我了解大部分代码,这是一本很棒的书,对我的编程很有帮助,我认为继承很容易,但我只是在这段代码中找不到它似乎在列表中的错误(这是任务所需),但我找不到问题所在。
#defines a class called Restaurant.
class Restaurant():
#defines the instances to be used in the class.
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
#defines the instance to show the cuisine.
def describe_restaurant(self):
print("\nThe Restaurant {0} sells {1} food.".format(self.restaurant_name.capitalize(), self.cuisine_type.capitalize()))
#this sets the number of customers served.
def set_number_served(self, customers):
self.number_served = customers
#this sets the increment for customers.
def increment_number_served(self, people):
self.number_served +=people
#defines the instance to show the Restaurant is open.
def open_restaurant(self):
print("The Restaurant {0} is Open.".format(self.restaurant_name.capitalize()))
#defines the instance to show the number of customers served.
def restaurant(self):
print("The Restaurant has served {0} people.\n".format(str(self.number_served).capitalize()))
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type="ice_cream"):
super().__init__(restaurant_name, cuisine_type)
self.flavours = []
def show_flavours(self):
print("\nWe have the following flavours available:")
for flavour in self.flavours:
print("--", flavour())
#makes the class ready to call.
res = Restaurant("McDonald's", "Fast")
rest = Restaurant("Prezzo's", "Italian")
resta = Restaurant("The Wok Place", "Chinese")
#calls the instances from the class.
res.describe_restaurant()
res.open_restaurant()
res.set_number_served(10000)
res.increment_number_served(300)
res.restaurant()
rest.describe_restaurant()
rest.open_restaurant()
rest.set_number_served(3000)
rest.increment_number_served(500)
rest.restaurant()
resta.describe_restaurant()
resta.open_restaurant()
resta.set_number_served(500)
resta.increment_number_served(100)
resta.restaurant()
bobs_ice = ("Bobs Ice")
bobs_ice.flavours = ["Vanilla", "Chocolate", "Pistachio", "Caramel"]
bobs_ice.describe_restaurant()
bobs_ice.show_flavours()
我不断收到此错误:
Traceback (most recent call last):
File "C:\Users\CJH-DESKTOP\Desktop\Python Activities\restaurant with classes.py", line 75, in <module>
bobs_ice.flavours = ["Vanilla", "Chocolate", "Pistachio", "Caramel"]
AttributeError: 'str' object has no attribute 'flavours'
【问题讨论】:
标签: python class inheritance