亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何以任何順序允許多個用戶響應?

如何以任何順序允許多個用戶響應?

素胚勾勒不出你 2021-11-23 16:15:22
我該怎么解決,以允許用戶輸入'Version','Credit','Info', 'Notes'和'Credit'以任何順序?這是我試過的代碼:if(answer == 'Debug'):    print('Type the word in the debug screen\nto get an output, '          'ex. Type "Version"\nto find the file Version.')print('\033[1;34;40m============')print('\033[1;39;40mVersion')print('\033[1;33;40mCredit')print('\033[1;34;40mInfo')print('\033[1;32;40mNotes')answer=raw_input()if(answer == 'Version'):    print('\033[1;39;39m1.2.2')if(answer == 'Credit'):    print('\033[1;33;40mXendos6 2/22/19')
查看完整描述

2 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

您可以使用包含每個問題和打印回復的字典,例如:


menu = {'Version': '1.2.2', 'Credit': 'Xendos6 2/22/19', 'Info': 'Some information'}


print('\033[1;34;40m============')

for k in menu.keys():

    print('\033[1;39;40m', k)


answer = raw_input()

if answer in menu:

    print('\033[1;39;39m', menu[answer])

else:

    print("Invalid answer:", answer)

這使得菜單更容易添加項目。如果要執行的代碼比簡單的文本字符串更復雜,則可以將操作放置在一個函數中,每個菜單項一個,并將函數名稱用作值。然后將該函數稱為menu[answer]()。


編輯:現在看來 OP 需要一個循環。這是一個示例,它在選擇時從菜單中刪除每個條目:


menu = {'Version': '1.2.2', 'Credit': 'Xendos6 2/22/19', 'Info': 'Some information'}


while menu:

    print('\033[1;34;40m============')

    for k in menu.keys():

        print('\033[1;39;40m', k)


    answer = raw_input()

    if answer in menu:

        print('\033[1;39;39m', menu[answer])

        del(menu[answer])

    else:

        print("Invalid answer:", answer)

這將在選擇項目時刪除鍵。循環在其中menu有數據項時繼續-為空while menu:時menu為假。


查看完整回答
反對 回復 2021-11-23
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

請看看這個。也許這會解決你的問題。


responses = {

    'version': '1.0.1',

    'credit': 'some_credit',

    'info': 'this is info',

    'debug': 'this is debug output'

}


for i in responses.keys():

    user_input = input('Please enter a choice from {}: '.format(options))

    print(responses.get(user_input))

注意:這只是一個模板


查看完整回答
反對 回復 2021-11-23
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號