1. 程式人生 > 其它 >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. 執行結果