我正在从事这个初学者项目,但出现错误

时间:2018-11-07 10:25:45

标签: python

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.name= restaurant_name
        self.cuisine = cuisine_type

    def describe_restaurant(self):
        print(self.name.title() + ' serves ' + self.cuisine + ' food.')

    def open_restaurant(self):
        print(self.name.title() + ' is now open. \nCome and Have some delicious ' +self.cuisine+ ' food.' )

restaurant= Restaurant('Big Chillo', 'Italian')
restaurant.describe_restaurant()
restaurant.open_restaurant()

class cuisine(Restaurant):
    def __init__(self, cuisine_type):
        self.name = cuisine_type

        super().__init__(cuisine_type)

    def availability(self):
        print ('These are the available cuisines ' + self.name.title())

menu =cuisine['Tiramisu \nCannoli \nPanna \ncotta \nCassata \nSemifreddo']
menu.availability()

文件“ D:/ python project / restaurant.py”,第25行,在 来吃一些美味的意大利美食。     菜单=美食['提拉米苏\ n卡诺利\ n潘纳\ ncotta \ n卡萨塔\ nSemifreddo'] TypeError:“类型”对象不可下标

2 个答案:

答案 0 :(得分:1)

使用括号()而不是方括号[]调用函数/类构造函数

menu = cuisine('Tiramisu \nCannoli \nPanna \ncotta \nCassata \nSemifreddo')

答案 1 :(得分:0)

发现您的代码有3个问题:
1.如@FHTMitchell所述,使用括号()而不是方括号[]
调用函数/类构造函数 2.您没有带1个参数的Restaurant Constructor,因此我在代码super().__init__("RestaurantName",cuisine_type)中添加了额外的参数
3. self.name是字符串,因此我们不应在将可用性方法从self.name()更改为self.name

的情况下将其作为函数打印
class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.name= restaurant_name
        self.cuisine = cuisine_type

    def describe_restaurant(self):
        print(self.name.title() + ' serves ' + self.cuisine + ' food.')

    def open_restaurant(self):
        print(self.name.title() + ' is now open. \nCome and Have some delicious ' +self.cuisine+ ' food.' )

restaurant= Restaurant('Big Chillo', 'Italian')
restaurant.describe_restaurant()
restaurant.open_restaurant()

class cuisine(Restaurant):
    def __init__(self, cuisine_type):
        self.name = cuisine_type

        super().__init__("hello",cuisine_type)

    def availability(self):
        print ('These are the available cuisines ' + self.name)

menu =cuisine('Tiramisu \nCannoli \nPanna \ncotta \nCassata \nSemifreddo')
menu.availability()
menu.describe_restaurant()

我得到的输出是:

Big Chillo serves Italian food.   
Big Chillo is now open.  
Come and Have some delicious Italian food.  
These are the available cuisines hello
Hello serves Tiramisu 
Cannoli 
Panna 
cotta 
Cassata 
Semifreddo food.

Process finished with exit code 0