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

Python测验程序运行时为何在问题下方输出None?

Python测验程序输出None的原因及解决方法

问题原因

你的代码里导致输出None的核心问题在这一行:

if questions[i].answer == input(print(questions[i].prompt)):

print()函数执行后没有返回值(默认返回None),当你把print(questions[i].prompt)作为input()的参数时,input()会将这个None当作提示内容输出,所以每个问题下方都会出现None。

解决方法

把打印提示和获取输入的操作分开,先打印问题,再单独调用input()获取用户答案:

score = 0
# 直接遍历questions列表,写法更简洁
for question in questions:
    print(question.prompt)
    user_answer = input()
    if question.answer == user_answer:
        print("Correct!")
        score += 1
    else:
        print("Incorrect!")
print("You got " + str(score) + "/3 correct")

修改后就不会再出现None,同时代码的可读性也更好。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 05:15:42