Python (闭合)局部变量';退出';作业前参考?
我正在Wing IDE上运行python 2.75 代码:Python (闭合)局部变量';退出';作业前参考?,python,Python,我正在Wing IDE上运行python 2.75 代码: exit = False while not exit: selection = int(raw_input("Press 1 to go and 0 to quit: ") if selection == 1: print("yay") elif selection == 0: print("Goodbye") exit = True else:
exit = False
while not exit:
selection = int(raw_input("Press 1 to go and 0 to quit: ")
if selection == 1:
print("yay")
elif selection == 0:
print("Goodbye")
exit = True
else:
print("Go away")
当我按0时,它会显示:
赋值前引用的局部变量“exit”
怎么了 您的代码工作正常,如下所示:
exit = False
while not exit:
selection = int(raw_input("Press 1 to go and 0 to quit: ")) #added ) to correct syntax error
if selection == 1:
print("yay")
elif selection == 0:
print("Goodbye")
exit = True
else:
print("Go away")
您的int()缺少一个接近的参数 不过,如果您使用的是while循环,为什么不简单地使用break而不是boolean呢?它干净多了。只需键入break,循环就会结束,而不是exit=True 如果我对您键入内容的理解是正确的,您希望以以下内容结束:
while True:
selection = int(raw_input("Press 1 to go and 0 to quit: "))
if selection == 1:
print("yay")
elif selection == 0:
print("Goodbye")
break
else:
print("Go away")
好的,不,他们不抱歉。祝贺你的10k代表身份;)你可以查看!并删除了答案;)@如果我的回答有什么问题,你能解释一下吗?谢谢。我没有否决投票,但我认为有必要作一些解释。目前,看起来您的代码与OP完全相同。这可能不是您的错,但无论如何,解释一下为什么某些代码修复了特定的问题是很好的。我很挑剔,考虑到以下问题,无论如何,你也没办法让它对社区有用:-)@juanchopanza,谢谢你,我已经对更正进行了评论