我試圖創建一個民意調查,用戶可以輸入自己的姓名和山區然后投票完成后,該代碼將列印“名稱想爬的山。” 但是,列印的山始終是最后一個用戶輸入的山。我可以知道如何糾正以便為不同的用戶列印不同的山脈嗎?謝謝。
我的代碼:
responses = {}
# set a flag to indicate that polling is active
polling_active = True
while polling_active:
# prompt for the person's name and response
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
# store the response in the dictionary
responses[name] = response
# find out if anyone else is going to take the poll
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False
# polling is complete. show the results.
print("\n--- Poll Results ---")
for name, reponse in responses.items():
print(name " would like to climb " response ".")
結果:
What is your name? Amelia
Which mountain would you like to climb someday? Everest
Would you like to let another person respond? (yes/ no) yes
What is your name? Bobby
Which mountain would you like to climb someday? Matterhorn
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
Amelia would like to climb Matterhorn.
Bobby would like to climb Matterhorn.
uj5u.com熱心網友回復:
你有一個錯字,在最后兩行,改變 reponse
for name, reponse in responses.items():
print(name " would like to climb " reponse ".")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/334486.html
