不是子木呀 发表于 2022-3-19 14:57:54

请问这一部分代码哪里出现问题了呢?Jupyter给的解释不太理解

class Restaurant:
    """一次模拟餐厅的简单尝试"""
   
    def _init_(self,restaurant_name,cuisine_type):
      self.restaurant_name = restaurant_name
      self.cuisine_type = cuisine_type
      
    def describe_restaurant(self):
      print(f"The restaurant name is {self.restaurant_name}.")
      print(f"The cuisine type is {self.cuisine_type}.")
      
    def open_restaurant(self):
      print("This restaurant is opening.")
      
restaurant_1 = Restaurant('a','b')

restaurant_1.describe_restaurant()
restaurant_1.open_restaurant()


TypeError                                 Traceback (most recent call last)
<ipython-input-15-6e85ab6df72d> in <module>()
   14         print("This restaurant is opening.")
   15
---> 16 restaurant_1 = Restaurant('self','a','b')
   17
   18 restaurant_1.describe_restaurant()

TypeError: Restaurant() takes no arguments

ckblt 发表于 2022-3-19 15:00:47

是 __init__ 不是 _init_ 。
class Restaurant:
    """一次模拟餐厅的简单尝试"""

    def __init__(self, restaurant_name, cuisine_type): # 注意,__init__旁边是两个下划线
      self.restaurant_name = restaurant_name
      self.cuisine_type = cuisine_type

    def describe_restaurant(self):
      print(f"The restaurant name is {self.restaurant_name}.")
      print(f"The cuisine type is {self.cuisine_type}.")

    def open_restaurant(self):
      print("This restaurant is opening.")


restaurant_1 = Restaurant("a", "b")

restaurant_1.describe_restaurant()
restaurant_1.open_restaurant()

深谙流年 发表于 2022-3-19 15:04:09

是__init__,每边两个下划线,不是一个下划线
页: [1]
查看完整版本: 请问这一部分代码哪里出现问题了呢?Jupyter给的解释不太理解