【问题标题】:Why wont my code work? seems to be an error with my list in my child class but cant work out what?为什么我的代码不起作用?我的孩子班的清单似乎有误,但无法解决什么问题?
【发布时间】: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


    【解决方案1】:

    这一行

    bobs_ice = ("Bobs Ice")
    

    应该是

    bobs_ice = IceCreamStand("Bobs Ice")
    

    现在,bobs_ice 是一个字符串,您不能为字符串分配新属性。

    【讨论】:

    • 现在似乎收到此错误:Traceback (most recent call last): File "C:/Users/CJH-DESKTOP/Desktop/Python Activities/restaurant with classes.py", line 78, in &lt;module&gt; bobs_ice.show_flavours() File "C:/Users/CJH-DESKTOP/Desktop/Python Activities/restaurant with classes.py", line 46, in show_flavours print("--", flavour()) TypeError: 'str' object is not callable
    【解决方案2】:
    bobs_ice = ("Bobs Ice")
    

    只是说bobs_ice 是一个字符串。显然,str.flavours 不是一个东西,所以你得到一个错误。 你可能的意思是:

    bobs_ice = IceCreamStand("Bobs Ice")
    

    【讨论】:

    • 完成了,但我现在似乎收到以下错误?
    • Traceback (most recent call last): File "C:/Users/CJH-DESKTOP/Desktop/Python Activities/restaurant with classes.py", line 78, in &lt;module&gt; bobs_ice.show_flavours() File "C:/Users/CJH-DESKTOP/Desktop/Python Activities/restaurant with classes.py", line 46, in show_flavours print("--", flavour()) TypeError: 'str' object is not callable
    • 将print("---", flavour()) 更改为print("---", flavour)。字符串不是函数,不能作为一个函数调用。
    【解决方案3】:

    您没有将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()
    

    【讨论】:

    • 现在似乎收到此错误:Traceback (most recent call last): File "C:/Users/CJH-DESKTOP/Desktop/Python Activities/restaurant with classes.py", line 78, in &lt;module&gt; bobs_ice.show_flavours() File "C:/Users/CJH-DESKTOP/Desktop/Python Activities/restaurant with classes.py", line 46, in show_flavours print("--", flavour()) TypeError: 'str' object is not callable
    • 这是print("--", flavour) 而不是print("--", flavour()),正如我在我的回答中发布的那样!您所要做的就是将代码复制粘贴到您的 IDE 中,然后运行它。
    猜你喜欢
    • 2021-04-26
    • 2019-11-21
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多