为什么我的代码不起作用?在我的孩子班中我的列表似乎是一个错误但是无法解决什么问题?

时间:2017-11-12 22:56:06

标签: python class inheritance

这是我的代码: 我可以在任务设置时改变任何东西,但我找不到我的错误?所有和任何帮助表示赞赏!

我理解大部分代码并且它是一本很棒的书,它确实帮助了我的编程,我认为继承非常简单,但我只是在这个代码中找不到它似乎在列表中的错误(这是任务所需的但是我找不到问题所在。

#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'

3 个答案:

答案 0 :(得分:0)

这一行

bobs_ice = ("Bobs Ice")

应该是

bobs_ice = IceCreamStand("Bobs Ice")

就像现在一样,bobs_ice是一个字符串,你不能为字符串分配新的属性。

答案 1 :(得分:0)

bobs_ice = ("Bobs Ice")

只是说bobs_ice是一个字符串。显然,str.flavours不是一件事,所以你得到一个错误。 你可能意味着:

bobs_ice = IceCreamStand("Bobs Ice")

答案 2 :(得分:0)

您未将bobs_ice实例化为IceCreamStand的实例:

你也在这里调用一个字符串print("--", flavour());请改为:print("--", flavour)

#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)     # <<== THERE, don't call a string

#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 = IceCreamStand("Bobs Ice")    #<<=== HERE!
bobs_ice.flavours = ["Vanilla", "Chocolate", "Pistachio", "Caramel"]

bobs_ice.describe_restaurant()  
bobs_ice.show_flavours()