其他分享
首页 > 其他分享> > TypeError: Restaurant() takes no arguments

TypeError: Restaurant() takes no arguments

作者:互联网

 

1. 错误描述

TypeError: Restaurant() takes no arguments

 

2. 原因:在编写__init__时,pycharm会自动添加关键字,有时会直接写称整型int, 即__int__。导致错误产生。

 

————————————————参考————————————————————————————————————————————————————————

3. 错误代码

# 9-1 restaurant
class Restaurant():
    def __int__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print("The " + self.restaurant_name + " have " +
              str(self.cuisine_type) + " kinds of food.")

    def open_restaurant(self):
        print("Now is opening.")

restaurant = Restaurant("'Restaurant of peace'", 108)
restaurant.describe_restaurant()
restaurant.open_restaurant()

  

4. 正确代码

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

    def describe_restaurant(self):
        print("The " + self.restaurant_name + " have " +
              str(self.cuisine_type) + " kinds of food.")

    def open_restaurant(self):
        print("Now is opening.")

restaurant = Restaurant("'Restaurant of peace'", 108)
restaurant.describe_restaurant()
restaurant.open_restaurant()

  

  

5.  执行结果

 

标签:__,cuisine,TypeError,restaurant,Restaurant,self,no,arguments,type
来源: https://www.cnblogs.com/kevin-hou1991/p/14932344.html