You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么我的Python投票程序跳过了询问是否新增用户的input函数

问题原因及修复方案

你的代码存在两处核心错误:

  • 缩进逻辑错误:Python依靠缩进划分代码块层级,你当前代码中存储投票结果、询问是否继续投票、修改轮询标志位的代码,全部没有缩进进while polling_active对应的循环体内。这就导致while循环只会反复执行循环内仅有的两行获取姓名和目标山峰的输入代码,永远不会执行到循环外的重复询问逻辑,因此会一直弹出询问姓名的输入框。
  • 字符串方法调用错误:你写的判断逻辑if repeat.lower == 'no'中,lower是字符串的内置方法,必须加括号写成repeat.lower()才会执行转小写的操作,否则你是在对比方法对象本身和字符串'no',永远返回False,即使修复缩进问题后,循环也无法正常退出。

修正后的完整代码

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('\nWould you like to let another person respond? (yes/no)')
    if repeat.lower() == 'no':
        polling_active = False

# Polling is complete.Show results.
print('---Poll Results---')
for name, response in responses.items():
    print(f'{name} would like to climb {response}.')

内容的提问来源于stack exchange,提问作者Mahi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:36:03