Class 不是 运行 - 没有结果

Class not running - getting no results

我是 python 类 的新手,我正在尝试 运行 此代码,但我没有得到任何结果:

class Restaurant:
    def __init__(self, mascalzone, it_fusion):
        self.mascalzone = mascalzone
        self.it_fusion = it_fusion

    def describe_restaurant(self):
        print(f"this restaurant is Italian and is named: {self.mascalzone}")

    def open_restaurant(self):
        print(f"the restaurant {self.it_fusion} is open , please come in!")

        # make  instance below:
        restaurant = Restaurant('open', 9)

        # printing two attributes individually:
        print(f"this:{restaurant.it_fusion}")
        print(f"that:{restaurant.mascalzone}")

        # calling both methods:
        restaurant.describe_restaurant()
        restaurant.open_restaurant()

那不是 class 的工作方式。您的代码的第一部分应该是描述 class,然后第二部分实际上是创建 class 的一个实例].所以要么你认为你必须在定义中定义 classes,要么你忘了缩进。

open_restaurant() 中,移除创建 Restaurant 的实例并将其放置在 class 定义之外。然后将调用这两种方法的代码也放在代码之外。您的其余代码没问题。

代码:

class Restaurant:
    def __init__(self, mascalzone, it_fusion):
        self.mascalzone = mascalzone
        self.it_fusion = it_fusion

    def describe_restaurant(self):
        print(f"this restaurant is Italian and is named: {self.mascalzone}")

    def open_restaurant(self):
        print(f"the restaurant {self.it_fusion} is open , please come in!")

restaurant = Restaurant('open', 9)
restaurant.open_restaurant()
restaurant.describe_restaurant()