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

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

遇到無(wú)限循環(huán)的麻煩(python)

遇到無(wú)限循環(huán)的麻煩(python)

Go
蝴蝶不菲 2022-12-27 10:08:10
嘿伙計(jì)們可以幫助這個(gè)循環(huán)它進(jìn)入第一個(gè)如果并且卡住感謝你的幫助Options = int(input('Enter an Options :'))while Options != 0:    if Options == 1:        item = input('enter the item : ')        qnty = int(input('Enter the Quantitiy for the item : '))        Shoping_list[item] = qnty    elif Options == 2:        for item in Shoping_list:            print(item, ':', Shoping_list[item])        item = input('Enter the item you want to Remove : ')        del(Shoping_list[item])    elif Options == 3:        for item in Shoping_list:            print(item, ':', Shoping_list[item])    elif Options != 0:        print('you didnt enter a valid number ')else:    print('shopping list is close')
查看完整描述

4 回答

?
慕蓋茨4494581

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

您的代碼有點(diǎn)難以閱讀且難以維護(hù)我建議,當(dāng)您想退出時(shí)將“while 循環(huán)”更改為無(wú)限循環(huán),只需打破循環(huán),我更喜歡在詢問(wèn)選項(xiàng)之前顯示菜單。


您可以像這樣更改代碼:


def display_menu():

    print("1. Add a new item to shopping list")

    print("2. Remove an item")

    print("3. Print Shopping List Items")

    print("0. Exit")

    return int(input('Enter an Options (0~3):'))



while True:

    option = display_menu()


    if option == 1:

        item = input('enter the item : ')

        qnty = int(input('Enter the Quantitiy for the item : '))

        Shoping_list[item] = qnty


    elif option == 2:

        for item in Shoping_list:

            print(item, ':', Shoping_list[item])

        item = input('Enter the item you want to Remove : ')

        del(Shoping_list[item])


    elif option == 3:

        for item in Shoping_list:

            print(item, ':', Shoping_list[item])


    elif option == 0:

        print('shopping list is close')

        break      # Exit menu


    else:    

        print('you didnt enter a valid number ')


查看完整回答
反對(duì) 回復(fù) 2022-12-27
?
慕妹3242003

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

太棒了,非常感謝你們!我是 python 的新手,這個(gè)信息非常有幫助


def display_menu():

    print("1. Add a new item to shopping list")

    print("2. Remove an item")

    print("3. Print Shopping List Items")

    print("0. Exit")

    return int(input('Enter an Options (0~3):'))



while True:

    option = display_menu()


    if option == 1:

        item = input('enter the item : ')

        qnty = int(input('Enter the Quantitiy for the item : '))

        Shoping_list[item] = qnty


    elif option == 2:

        for item in Shoping_list:

            print(item, ':', Shoping_list[item])

        item = input('Enter the item you want to Remove : ')

        del(Shoping_list[item])


    elif option == 3:

        for item in Shoping_list:

            print(item, ':', Shoping_list[item])


    elif option == 0:

        print('shopping list is close')

        break      # Exit menu


    else:    

        print('you didnt enter a valid number ')

Ps喜歡帶有您可以調(diào)用的功能的選項(xiàng)


查看完整回答
反對(duì) 回復(fù) 2022-12-27
?
肥皂起泡泡

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

在每個(gè) if 語(yǔ)句中,在末尾插入 Options = 0。由于您的 while 循環(huán)取決于不為 0 的選項(xiàng)。將其重置為 0 允許用戶選擇另一個(gè)選項(xiàng)。


while Options != 0:

    if Options == 1:

        item = input('enter the item : ')

        qnty = int(input('Enter the Quantitiy for the item : '))

        Shoping_list[item] = qnty

        Options = 0

另外,作為提示,請(qǐng)確保您的拼寫(xiě)和語(yǔ)法準(zhǔn)確無(wú)誤,并且間距保持一致。它使其他人更容易閱讀您的代碼。


這是正確的 if 循環(huán)的工作示例。用戶可以用 if 循環(huán)修改字典,并且可以一個(gè)接一個(gè)地運(yùn)行它們。


Shoping_list = {}

while True:

    Options = int(input('Enter an Options :'))


    while Options != 0:

        if Options == 1:

            item = input('enter the item : ')

            qnty = int(input('Enter the Quantitiy for the item : '))

            Shoping_list[item] = qnty

            Options = 0


        elif Options == 2:

            for item in Shoping_list:

                print(item, ':', Shoping_list[item])

            item = input('Enter the item you want to Remove : ')

            del(Shoping_list[item])

            Options = 0


        elif Options == 3:

            for item in Shoping_list:

                print(item, ':', Shoping_list[item])

            Options = 0


        elif Options != 0:

            print('you didnt enter a valid number ')


    else:

        print('shopping list is close')


查看完整回答
反對(duì) 回復(fù) 2022-12-27
?
人到中年有點(diǎn)甜

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

Options = int(input('Enter an option'))在 while 循環(huán)中插入第一條語(yǔ)句。


while options!=0:

  Options = int(input('Enter an option'))

.

.

.

.


查看完整回答
反對(duì) 回復(fù) 2022-12-27
  • 4 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(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)