跳出循环 - Python

Breaking out of loop - Python

我已经尝试在 SO 上进行谷歌搜索和搜索,但我无法弄清楚为什么我在倒数第二行的 break 没有跳出 while 循环。更好的是,我无法弄清楚为什么循环也没有继续。我的目的是让用户有可能在最后一次选择后前往主菜单(基本上是 menuchoice 的 while 循环(这是我在此处粘贴的循环上方的一个循环)。

有什么建议吗?先感谢您。感觉好像缺少了一些重要的东西。

#this is going to show how many exercise weapons you need for next magic level
if 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))) + ".")

                if trainingdummy == "n" or trainingdummy == "N":
                    #list the different exercise weapons
                    print("Select exercise weapon:")
                    print("1. Training rod")

                    #loop where ask user to input what exercise weapon they want to calculate
                    while True:
                        weaponchoice = input()
                        #User choice
                        if weaponchoice == "1":
                            print("The amount of training rods needed for next magic level is " + str((nextmaglvlpal(num1))) + ".")

                        elif weaponchoice == "f":
                            break

                        print("\nGo to main menu? Press F.")

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

我想

会对你有所帮助。 Break 仅从当前循环中断。如果你想升级,你需要分别打破每个循环。

A suggestion 是将一个循环变成一个函数并使用 return 这将有效地退出任何循环。不过需要进行一些代码重构。

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

首先,你应该在input()命令中打印一些东西,因为它会被清除:input("Text to display").

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

因为在 Python 中没有 goto 指令或命名循环,您可以使用标志。当用户按下 'F' 时,标志设置为真,然后在每个外部嵌套循环的开头使用此标志来中断它们。它可能看起来像这样:

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