2 回答

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為假。

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))
注意:這只是一個模板
添加回答
舉報