簡體   English   中英

跳出循環 - Python

[英]Breaking out of loop - Python

我試過谷歌搜索和搜索 SO,但我無法弄清楚為什么我在倒數第二行的中斷沒有離開 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.")

我認為會幫助你。 Break 僅從當前循環中斷。 如果您想提升級別,則需要分別從每個循環中中斷。

一個建議是將循環轉換為函數並使用 return 有效地退出任何循環。 不過,將需要一些代碼重構。

如果不是這種情況,您能否提供更多信息和完整代碼(我們在這里沒有看到更高的循環?)

weaponchoice == "1"添加一個break以退出循環。

首先,您應該在input()命令中打印內容,因為它會在 intent: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM