在 class 语句中调用方法,在 if/elif 语句中
Calling a method in a class, in an if/elif statement
我正在尝试创建一个项目,但我 运行 遇到了错误。
这是我的代码(不是全部,它很长,但我 运行 遇到的问题):
class Rest(object):
def __init__(self, name, order=[], total=0):
self.name = name
self.order = []
self.total = 0
def end_order(self):
print("Here is your complete order: {0}".format(self.order))
print("Here is your total: {0}".format(self.total))
def order_menu(self):
loop = 1
while (loop == 1):
question_1 = raw_input("What would you like? Push S to Submit, Push C to Cancel")
if (question_1 == "1"):
self.total += 4.99
print("You added a cheeseburger, .99)
elif (question_1 == "S"):
end_order()
好吧,所以在order_menu(self)下,在elif语句下,报错了:
"Global name 'end_order()' is not defined"。
可能有些愚蠢的事情我没有做,但我不知道是什么..
我认为调用 class 方法时 Python 中需要 self
关键字。尝试:
self.end_order()
我正在尝试创建一个项目,但我 运行 遇到了错误。
这是我的代码(不是全部,它很长,但我 运行 遇到的问题):
class Rest(object):
def __init__(self, name, order=[], total=0):
self.name = name
self.order = []
self.total = 0
def end_order(self):
print("Here is your complete order: {0}".format(self.order))
print("Here is your total: {0}".format(self.total))
def order_menu(self):
loop = 1
while (loop == 1):
question_1 = raw_input("What would you like? Push S to Submit, Push C to Cancel")
if (question_1 == "1"):
self.total += 4.99
print("You added a cheeseburger, .99)
elif (question_1 == "S"):
end_order()
好吧,所以在order_menu(self)下,在elif语句下,报错了: "Global name 'end_order()' is not defined"。
可能有些愚蠢的事情我没有做,但我不知道是什么..
我认为调用 class 方法时 Python 中需要 self
关键字。尝试:
self.end_order()