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

Python开发英语语法学习小游戏输入正确仍返回错误的问题咨询

问题解答

1. 输入正确仍判断错误的原因

核心错误是变量引用偏差:你通过input()获取到的用户输入已经存储到了question1变量中,但判断条件里误写了Python内置函数名input,而非存储用户输入的变量名question1。
Python会将input识别为内置的输入函数对象,和字符串'went'做对比永远返回假,所以程序始终输出incorrect。
修正后的单题代码参考:

def simple_past():
    question1 = input('I ____ to the park. Input: ').lower()  
    if question1 == 'went':
        print('correct')
    else:
        print('incorrect')

2. 这类练习功能的更优实现方案

可以从可扩展性、易用性角度优化,核心思路如下:

  • 把题目、正确答案抽离为独立的配置列表,不需要为每道题单独写函数,后续新增题目只需要修改配置即可
    参考实现代码:
# 题库配置格式:(题目描述, 正确答案)
grammar_questions = [
    ('I ____ to the park yesterday.', 'went'),
    ('She ____ a cake for me last week.', 'made'),
    ('They ____ football last weekend.', 'played')
]

def run_grammar_practice(question_list):
    score = 0
    total_count = len(question_list)
    for idx, (question, right_answer) in enumerate(question_list, 1):
        user_input = input(f'Q{idx}: {question} Input: ').strip().lower()
        if user_input == right_answer:
            print('correct')
            score += 1
        else:
            print(f'incorrect,正确答案是:{right_answer}')
    print(f'练习结束,本次得分:{score}/{total_count}')

# 启动练习
run_grammar_practice(grammar_questions)
  • 补充输入兼容处理:自动忽略用户输入的首尾空格、统一转小写,避免输入格式问题导致的误判
  • 可按需拓展错题记录、专项题型分类、正确率统计等功能,提升学习体验

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 04:15:02