在菜单上选择 2 作为我的选择后,当我输入 1 作为选项时,程序会打印“感谢使用.....再见”,然后循环回到菜单而不是停止。我无法弄清楚原因是什么,因为我多次尝试修复它但失败了。请告诉我该怎么做。谢谢
代码: http: //pastebin.com/kc0Jk9qY
抱歉,我无法实现此评论中的代码,这很麻烦。
在菜单上选择 2 作为我的选择后,当我输入 1 作为选项时,程序会打印“感谢使用.....再见”,然后循环回到菜单而不是停止。我无法弄清楚原因是什么,因为我多次尝试修复它但失败了。请告诉我该怎么做。谢谢
代码: http: //pastebin.com/kc0Jk9qY
抱歉,我无法实现此评论中的代码,这很麻烦。
while n!=1:
如果在一个循环中,您的所有代码。n=1
当你想退出时设置,就是这样。因此,只需将您的代码更改为以下内容,它将停止:
elif endex==1:
print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
time.sleep(1)
error2=False
n=1
break
只会终止最里面的循环,因此需要确保所有外部循环也被终止。
编辑 更改的代码是:
if choice==2:
while error2:
try:
endex=input("Do you want to Exit? \nInput (1)y or (2)n: ")
if endex== 0 or endex >=3:
print"Try again"
if endex==2:
print "You have chosen to run this programme again...\n"
if endex==1:
print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
time.sleep(1)
error2=False
n=1
您提供的内容看起来是正确的。那break
会让你脱离while error2
循环。所以问题必须在那之后。我希望那if choice==2
是在另一个循环中,而您并没有摆脱它。
好的,就是这样。您的 error2 值是不必要的。只需启动一个无限循环并突破有效响应。我也会考虑在你的其他循环中这样做。
if choice==2:
while True:
try:
endex=input("Do you want to Exit? \nInput (1)y or (2)n: ")
if endex== 0 or endex >=3:
print"Try again"
if endex==2:
print "You have chosen to run this programme again...\n"
break
if endex==1:
print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
time.sleep(1)
n=1
break