我正在尝试创建一个民意调查,用户可以输入他们的名字和山,然后当民意调查完成时,代码将打印">名称想爬山。"但是,打印的mountain始终是最后一个用户输入的mountain。我可以知道如何纠正,使它打印不同的山为不同的用户?谢谢。
我代码:
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.
你有一个错别字,在最后两行,更改reponse
for name, reponse in responses.items():
print(name + " would like to climb " + reponse + ".")