我的函数没有显示number_served的输入数字:
class Restaurant():
def __init__(self, name, cuisine):
self.name = name
self.cuisine = cuisine
self.number_served = 0
def describe_rest(self):
print("Welcome to " + self.name.title() + "!")
print("We serve the best " + self.cuisine.title() + " food!")
def open_rest(self):
print(self.name.upper() + " is OPEN now!")
def set_number_served(self, number_served):
print("We have served " + str(self.number_served)
+ " customers today.")
def increment_number_served(self, additional_served):
print("We have served " + str(self.number_served)
+ str(self.additional_served) + " customers today.")
restaurant = Restaurant('gamja', 'korean')
print("\n")
restaurant.describe_rest()
print("\n")
restaurant.open_rest()
restaurant.set_number_served(1000)
在最后一行restaurant.set_number_served(1000)
,我得到“我们今天已经为0个客户提供服务”。而不是1000。我在做什么错了?
答案 0 :(得分:4)
您忘记在该例程中设置实例变量。只需添加一行即可。
def set_number_served(self, number_served):
self.number_served = number_served
print("We have served " + str(self.number_served)
+ " customers today.")