繁体   English   中英

Python循环无法正常工作

[英]Python loop not working as intended

在菜单上选择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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM