【发布时间】:2021-07-15 21:11:15
【问题描述】:
我是 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()
【问题讨论】:
-
缩进很重要...
-
这段代码除了定义一个类之外什么也没做。您必须创建它的实例并调用方法才能运行代码。
-
@KalusD。函数被调用,但它们缩进到第二个函数中
-
从
# make instance below开始的所有内容都需要在左边距。 -
我猜 Uri 在创建问题时弄乱了缩进。提示:粘贴代码,标记它,然后按
{}按钮。
标签: python class methods attributes