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

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

如果二維列表的元素不在范圍內(nèi),如何處理錯誤

如果二維列表的元素不在范圍內(nèi),如何處理錯誤

素胚勾勒不出你 2023-02-15 17:34:34
我正在嘗試錯誤處理 doughnuts_gone 的輸入。用戶應(yīng)輸入列表的元素位置。例如,如果他們想以 18 美元的價格刪除 6 個奶油甜甜圈的訂單,那么您應(yīng)該輸入列表的位置;先列出so位置0,然后把2d列表中的列表元素位置去掉。我的程序的問題是,如果輸入的數(shù)字不在列表中可用元素的范圍內(nèi),我不知道如何編寫 elif 語句:elif i != total_order[i]:簡化程序:def check_valid(prompt):        while True:            try:                i = int(input(prompt))                if i == '':                    print("You must enter a value for the doughnut item you would like to change.")                    print()                elif i != total_order[i]:                    print("Invalid")                else:                    break            except:                break        return itotal_order = [['Cream',6,18], ['Cookies',5,20], ['Jam',6,16]]for i in range(len(total_order)):    print("{}     {} {} Doughnuts = ${:.2f}".format(i, total_order[i][1],total_order[i][0],total_order[i][2]))doughnuts_gone = check_valid("Enter the number associated with the doughnut order you would like to remove? ")謝謝你!我希望這是有道理的!:)
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊

您將i變量檢查為字符串 ( if i == '':),但它在前一行 ( i = int(input(prompt))) 中被強(qiáng)制轉(zhuǎn)換為整數(shù)。您應(yīng)該檢查列表的長度而不是這一行:elif i != total_order[i]:。由于這個問題,您的腳本無法正常工作。我寫了一個工作代碼,我已經(jīng)測試了它。請參閱下面我的代碼/測試。


此外,您的貨車會改進(jìn)您的代碼(例如:檢查輸入是否可以轉(zhuǎn)換為整數(shù)。)。


代碼:


def check_valid(prompt):

    while True:

        try:

            i = input(prompt)

            if not i:

                print("You must enter a value for the doughnut item you would like to change.")

                print()

            elif int(i) > len(total_order)-1:

                print("Invalid")

            else:

                break

        except:

            break

    return int(i)



total_order = [['Cream', 6, 18], ['Cookies', 5, 20], ['Jam', 6, 16]]


for i in range(len(total_order)):

    print("{}     {} {} Doughnuts = ${:.2f}".format(i, total_order[i][1], total_order[i][0],

                                                    total_order[i][2]))

doughnuts_gone = check_valid(

    "Enter the number associated with the doughnut order you would like to remove? ")

print("Valid value! Selected one: {}".format(total_order[doughnuts_gone]))

輸出:


>>> python3 test.py

0     6 Cream Doughnuts = $18.00

1     5 Cookies Doughnuts = $20.00

2     6 Jam Doughnuts = $16.00

Enter the number associated with the doughnut order you would like to remove? 3

Invalid

Enter the number associated with the doughnut order you would like to remove? 

You must enter a value for the doughnut item you would like to change.


Enter the number associated with the doughnut order you would like to remove? 0

Valid value! Selected one: ['Cream', 6, 18]


查看完整回答
反對 回復(fù) 2023-02-15
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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