为了账号安全,请及时绑定邮箱和手机立即绑定

跳出循环 - Python

跳出循环 - Python

呼唤远方 2022-07-19 16:35:57
我已经尝试在 SO 上进行谷歌搜索和搜索,但我无法弄清楚为什么我在倒数第二行的中断没有退出 while 循环。更好的是,我无法弄清楚为什么循环也没有继续。我的意图是让用户有可能在最后一次选择之后前往主菜单(基本上是 menuchoice 的 while 循环(这是我在此处粘贴的一个循环)。有什么建议么?先感谢您。感觉就像我错过了一些必不可少的东西。#this is going to show how many exercise weapons you need for next magic levelif menuchoice == "4":    #these functions returns the amount of magic wands/rods that is needed to be spent for next magic level    print("Select vocation")    print("Press P for Royal Paladin")    #ask user to input vocation:    while True:        vocationchoice = input()        if vocationchoice == "P" or vocationchoice == "p":            #ask user to input magic level for paladin            num1 = float (input("Enter your magic level: "))            #ask for own training dummy            print("Do you have your own exercise dummy? Type Y for yes and N for no.")            while True:                trainingdummy = input()                if trainingdummy == "y" or trainingdummy == "Y":                    #list the different exercise weapons                    print("Select exercise weapon:")                    print("1. Training rod")                    #loop, where we ask user to input what exercise weapon they want to calculate                    while True:                        while True:                            weaponchoice = input()                            if weaponchoice == "q":                                sys.exit() #quit the program                            if weaponchoice == "1" or weaponchoice == "2" or weaponchoice == "3" or weaponchoice == "f":                                break #break out of the input loop                        #User choice                        if weaponchoice == "1":                            print("The amount of training rods needed for next magic level is " + str((nextmaglvlpalwithdummy(num1))) + ".")
查看完整描述

3 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

我想这会对你有所帮助。Break 仅从当前循环中断。如果您想提高水平,则需要分别从每个循环中中断。

一个建议是将循环转换为函数并使用 return 将有效地退出任何循环。不过,需要进行一些代码重构。

如果不是这种情况,您是否可以提供更多信息和完整代码(我们在这里看不到更高的循环?)


查看完整回答
反对 回复 2022-07-19
?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

首先,您应该在命令中打印内容,input()因为它会在 intent: 中被清除input("Text to display")。


其次,如果你想退出到主菜单,你需要打破每个嵌套循环。在这里你只打破最内层的循环。


在 Python 中没有goto指令也没有命名循环,您可以使用标志。当 used 按下“F”时,标志设置为 true,然后在每个外部嵌套循环的开头使用该标志来中断它们。它看起来像这样:


while True: # This is your menu loop

  menuFlag = False # Declare and set the flag to False here


  menu = input("Choose the menu: ")

  # ...


  while True: # Choose character loop

    if menuFlag: break  # Do not forget to break all outer loops   


    character = input("Choose a character: ")

    # ...


    while True: # Any other loop (choose weapon, ...)

        weapon = input("Choose weapon: ")


        # Here you want to return to the menu if f is pressed

        # Set the flag to True in this condition

        if weapon == "f":

            menuFlag = True

            break


在您的游戏中,这类似于:


goToMainMenu = False


while True:

    if goToMainMenu: break

    vocationchoice = input("Select vocation.\nPress P for Royal Paladin: ")


    if vocationchoice == "P" or vocationchoice == "p":

        #ask user to input magic level for paladin

        num1 = float (input("Enter your magic level: "))


        #ask for own training dummy

        while True:

            if goToMainMenu: break


            trainingdummy = input("Do you have your own exercise dummy?\nType Y for yes and N for no: ")

            if trainingdummy == "y" or trainingdummy == "Y":

                #loop, where we ask user to input what exercise weapon they want to calculate

                while True:

                    while True:

                        weaponchoice = input("Select exercise weapon:\n1. Training rod: ")

                        if weaponchoice == "q":

                            sys.exit() #quit the program

                        if weaponchoice == "1" or weaponchoice == "2" or weaponchoice == "3" or weaponchoice == "f":

                            break #break out of the input loop


                    #User choice

                    if weaponchoice == "1":

                        print("The amount of training rods needed for next magic level is " + str((nextmaglvlpalwithdummy(num1))) + ".")


            if trainingdummy == "n" or trainingdummy == "N":

                #list the different exercise weapon


                #loop where ask user to input what exercise weapon they want to calculate

                while True:

                    weaponchoice = input("Select exercise weapon (press F for main menu):\n1. Training rod: ")

                    #User choice

                    if weaponchoice == "1":

                        print("The amount of training rods needed for next magic level is " + str((nextmaglvlpalwithdummy(num1))) + ".")


                    elif weaponchoice == "f" or weaponchoice == "F":

                        goToMainMenu = True

                        break


查看完整回答
反对 回复 2022-07-19
?
四季花海

TA贡献1811条经验 获得超5个赞

添加一个breakforweaponchoice == "1"以跳出循环。



查看完整回答
反对 回复 2022-07-19
  • 3 回答
  • 0 关注
  • 164 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号