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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

方法下的無(wú)限循環(huán)“try and if”

方法下的無(wú)限循環(huán)“try and if”

忽然笑 2023-06-27 17:27:45
執(zhí)行menu()并轉(zhuǎn)到 1、2 或 3 就可以很好地完成其工作。但是經(jīng)過之后getproduct(character)再返回,menu()如果你選擇3,就會(huì)形成一個(gè)糟糕的循環(huán)。我想知道為什么,以及如何解決這個(gè)問題......def menu():    menu = '1. ice\n2. cream\n3. quit'    print(menu)        try:        order = int(input('choose one: '))                if order == 1:            c = 'ice'            getproduct(c)        elif order == 2:            c = 'cream'            getproduct(c)                    elif order == 3:            exit()                    else: menu()            except ValueError: menu()def getproduct(character):    toping = int(input('1. ice or 2. cream?'))        try:        if character == 'ice' and toping == 1:            print(character + 'ice')            menu()                elif character == 'ice' and toping == 2:            print(character + 'cream')            menu()        elif character == 'cream' and toping == 1:            print(character + 'ice')            menu()        elif character == 'cream' and toping == 2:            print(character + 'cream')            menu()        else: getproduct(character)    except: getproduct(character)            menu()
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

代碼中有幾個(gè)錯(cuò)誤。首先,您使用的是 exit,不應(yīng)在文件中使用它,相反,我在示例中使用具有相同目標(biāo)的模塊 sys (sys.exit(0))。


另一方面,您以不精確的方式使用輸入檢查,并且循環(huán)應(yīng)該有所不同。在菜單中,我個(gè)人推薦使用 while 循環(huán)。


您的問題的解決方案和一些改進(jìn)(可能更好):


import sys


def menu():

    menu = '1. ice\n2. cream\n3. quit'


    while True:

        print(menu)

        try:

            order = int(input('choose one: '))

        except:

            print("Use a correct answer")

        else:

            if order == 1:

                c = 'ice'

                getproduct(c)

            elif order == 2:

                c = 'cream'

                getproduct(c)

            elif order == 3:

                sys.exit(0)

            else:

                print("Use a correct answer")


def getproduct(character):

    topings = '1. ice or 2. cream?: '


    while True:

        print(topings)

        try:

            second_order = int(input())

        except:

            print("Use a correct answer")

        else:

            if character == 'ice' and second_order == 1:

                print(character + 'ice')

                break

            elif character == 'ice' and second_order == 2:

                print(character + 'cream')

                break

            elif character == 'cream' and second_order == 1:

                print(character + 'ice')

                break

            elif character == 'cream' and second_order == 2:

                print(character + 'cream')

                break

            else:

                print("Use a correct answer.")


menu()


查看完整回答
反對(duì) 回復(fù) 2023-06-27
?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

該exit()函數(shù)通過引發(fā)異常類型來(lái)工作SystemExit,異常類型沿著鏈向上傳播,直到?jīng)]有更多的東西可以運(yùn)行并且程序停止。


這是一個(gè)例外,意味著上面引發(fā)的代碼SystemExit允許關(guān)閉資源并執(zhí)行其他結(jié)束活動(dòng),以免破壞任何外部資源。然而,這也意味著空白except:語(yǔ)句可以捕獲SystemExit并忽略它。這就是這里發(fā)生的事情。


def getproduct(character):


    toping = int(input('1. ice or 2. cream?'))

    

    try:

        ...

        # call to menu(), which calls exit()

    except: # this activates on ANY exception, including SystemExit

        getproduct(character)

一般來(lái)說,你幾乎不應(yīng)該使用原始except塊,因?yàn)樵谶@種情況下它會(huì)捕獲你不希望它捕獲的東西。相反,分析 中的代碼try,找出它將拋出什么類型的異常,并捕獲這些異常。在你的情況下,它可能是ValueError或者TypeError:


try:

    ...

except (ValueError, TypeError):

    getproduct(character)

或者,如果您決心捕獲所有內(nèi)容,則可以為錯(cuò)誤是 a 編寫一個(gè)特殊的異常SystemExit(盡管,空白except:或 aexcept Exception:被認(rèn)為是不好的做法):


try:

    ...

except SystemExit:

    pass  # ignore it

except:

    getproduct(character)

根據(jù)文檔,您應(yīng)該注意:


quit(code=None) exit(code=None) 在打印時(shí),打印一條消息,如“使用 quit() 或 Ctrl-D(即 EOF)退出”,并在調(diào)用時(shí),使用指定的退出代碼引發(fā) SystemExit。


您可能應(yīng)該使用sys.exit()它,盡管它的作用基本相同。


查看完整回答
反對(duì) 回復(fù) 2023-06-27
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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