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

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

使用非關(guān)鍵字在Python中的For循環(huán)出現(xiàn)問題

使用非關(guān)鍵字在Python中的For循環(huán)出現(xiàn)問題

慕斯709654 2021-03-29 09:11:37
我對(duì)此進(jìn)行了大量研究,但無法弄清楚自己在做什么錯(cuò)。請(qǐng)幫忙?。?!程序的目標(biāo): 一直問用戶選擇一個(gè)選項(xiàng)的問題,直到用戶在提供的列表中選擇一個(gè)為止。 問題:我只是無法使not關(guān)鍵字正常工作。代碼語法:items = {'1': '2', '3': '4', '5': '6'}choice = input("Select your item: ")print(choice)for choice not in items:    if choice in items:        the_choice = items[choice]        print("You chose",the_choice)        break    else:        print("Uh oh, I don't know about that item")日食錯(cuò)誤:for not choice in items:          ^SyntaxError: invalid syntax
查看完整描述

3 回答

?
慕工程0101907

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

試試這個(gè):


items = {'1': '2', '3': '4', '5': '6'}


while True:

    choice = input("Select your item: ")

    print(choice)

    if choice in items:

        the_choice = items[choice]

        print("You chose",the_choice)

        break


    print("Uh oh, I don't know about that item")


查看完整回答
反對(duì) 回復(fù) 2021-04-13
?
心有法竹

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

只是從您的代碼中忽略不是關(guān)鍵字


items = {'1': '2', '3': '4', '5': '6'}

    choice = input("Select your item: ")

    print(choice)

    for choice  in items:

       if choice in items:

        the_choice = items[choice]

        print("You chose",the_choice)

        break

    else:

        print("Uh oh, I don't know about that item")


查看完整回答
反對(duì) 回復(fù) 2021-04-13
?
拉風(fēng)的咖菲貓

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

讓我們來看看為什么當(dāng)它不應(yīng)該工作完全忽視了語法部分:


items由組成{'1': '2', '3': '4', '5': '6'}。itemssay的補(bǔ)碼citems應(yīng)包含所有字符串,但不包括items。


for not choice in items就像在說for choice in citems。對(duì)于解釋器來說這沒有意義,因?yàn)樵谶@里定義這么大的集合確實(shí)是一個(gè)問題。


但是,您的問題可以通過以下方法解決:


items = {'1': '2', '3': '4', '5': '6'}

choice = input("Select your item: ")


while choice not in items:

    print("Uh oh, I don't know about that item")

    choice = input("Select your item: ")


the_choice = choice #assuming you want to place the value of `choice` in `the_choice` for some reason

print("You chose",the_choice)


查看完整回答
反對(duì) 回復(fù) 2021-04-13
  • 3 回答
  • 0 關(guān)注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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