第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

跳出循環(huán) - Python

跳出循環(huán) - Python

呼喚遠方 2022-07-19 16:35:57
我已經(jīng)嘗試在 SO 上進行谷歌搜索和搜索,但我無法弄清楚為什么我在倒數(shù)第二行的中斷沒有退出 while 循環(huán)。更好的是,我無法弄清楚為什么循環(huán)也沒有繼續(xù)。我的意圖是讓用戶有可能在最后一次選擇之后前往主菜單(基本上是 menuchoice 的 while 循環(huán)(這是我在此處粘貼的一個循環(huán))。有什么建議么?先感謝您。感覺就像我錯過了一些必不可少的東西。#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條經(jīng)驗 獲得超6個贊

我想這會對你有所幫助。Break 僅從當前循環(huán)中斷。如果您想提高水平,則需要分別從每個循環(huán)中中斷。

一個建議是將循環(huán)轉(zhuǎn)換為函數(shù)并使用 return 將有效地退出任何循環(huán)。不過,需要進行一些代碼重構(gòu)。

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


查看完整回答
反對 回復 2022-07-19
?
白豬掌柜的

TA貢獻1893條經(jīng)驗 獲得超10個贊

首先,您應該在命令中打印內(nèi)容,input()因為它會在 intent: 中被清除input("Text to display")。


其次,如果你想退出到主菜單,你需要打破每個嵌套循環(huán)。在這里你只打破最內(nèi)層的循環(huán)。


在 Python 中沒有g(shù)oto指令也沒有命名循環(huán),您可以使用標志。當 used 按下“F”時,標志設置為 true,然后在每個外部嵌套循環(huán)的開頭使用該標志來中斷它們。它看起來像這樣:


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條經(jīng)驗 獲得超5個贊

添加一個breakforweaponchoice == "1"以跳出循環(huán)。



查看完整回答
反對 回復 2022-07-19
  • 3 回答
  • 0 關(guān)注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號