为什么我的Python字典循环后不显示结果?

# 使用用户输入填充字典
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运行的问题

阅读 1.1k
avatarAI BotBETA

你的代码中存在两个关键问题:

  1. 数据未存入字典:你收集了namereponse,但没有将它们存入responses字典
  2. 变量名拼写错误reponse应为response(少了一个's')

以下是修复后的代码:

responses = {}
polling_active = True

while polling_active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")  # 修正拼写
    
    # 关键修复:将数据存入字典
    responses[name] = response  # 添加这行代码
    
    repeat = input("Would you like to let another person respond? (yes/no)")
    if repeat.lower() == 'no':  # 添加.lower()处理大小写
        polling_active = False

print("\n--- Poll Results ---")
for name, response in responses.items():
    print(f"{name} would like to climb {response}.")

修复说明:

  1. 添加字典赋值responses[name] = response 将用户输入存储到字典中
  2. 修正变量名:将 reponse 改为 response 保持一致性
  3. 增强健壮性:使用 repeat.lower() 使输入接受 "NO"/"No"/"no" 等格式

为什么原代码不报错但无输出?

  • 字典 responses 始终为空(因为没有添加任何数据)
  • 空字典的 items() 循环不会执行任何操作
  • Python 不会对空循环报错,属于逻辑错误而非语法错误

修复后程序会正常显示所有用户输入的结果,例如:

--- Poll Results ---
Alice would like to climb Everest.
Bob would like to climb Kilimanjaro.
4 个回答

根据你原来代码的意图,你是想创建一个空字典 responses, 然后使用name(通过input方法获得的字符串)作为这个字典中的键,并使用response(通过input方法获得的字符串)作为name键的值,在while 循环中让用户不断地输入并将结果添加到字典中
你的代码print("\n--- Poll Results ---") 在while 循环退出后一定会运行的,请确认你在控制台看到的输出。

你这个问题其实很简单——代码逻辑没错,但你根本没把数据存进字典里。
根本没有内容可以遍历,循环自然就“没有运行”,但也不会报错。
新手这种简单问题问ai会更高效,

responses = {}
polling_active = True

while polling_active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")

    # ⭐ 把数据存入字典
    responses[name] = response

    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}.")
responses = {}
polling_active = True

while polling_active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")

    # 将回答存储到字典中(关键缺少的步骤)
    responses[name] = response

    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}.")

你的代码 没有将输入存入字典 responses,后面循环读取 responses 自然什么也没有。

在输入后通过 responses[name] = reponse 写入字典即可:

# 使用用户输入填充字典
responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True

while polling_active:
    # 提示输入被调查者的名字和问答
    name = input("\nWhat is your name? ")
    reponse = input("Which mountain would you like to climb someday? ")

    # 存入字典
    responses[name] = reponse

    # 将回答存储在字典中
    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}.")
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题