# 使用用户输入填充字典
responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和问答
name = input("\nWhat is your name? ")
reponse = input("Which mountain would you like to climb someday? ")
# 将回答存储在字典中
repeat = input("Would you like to let another person respond? (yes/no)")
if repeat == 'no':
polling_active = False
# 调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(f"{name} would like to climb {response}.")倒数第二个print可以运行,但倒数第一个print没有运行,编程软件也没有报错
解决最后一个print运行的问题
根据你原来代码的意图,你是想创建一个空字典 responses, 然后使用name(通过input方法获得的字符串)作为这个字典中的键,并使用response(通过input方法获得的字符串)作为name键的值,在while 循环中让用户不断地输入并将结果添加到字典中
你的代码print("\n--- Poll Results ---") 在while 循环退出后一定会运行的,请确认你在控制台看到的输出。