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

Python测验游戏最终分数计算代码错误修正求助

修正Python测验游戏的分数计算逻辑错误

你的代码主要有三个核心问题导致最终分数计算失败:

  • 分数变量被定义为字符串(如'10'),字符串相加是拼接操作,无法实现数值累加;同时用字符串和布尔值True比较完全不成立,逻辑判断彻底失效。
  • 没有跟踪用户实际答对的题目,始终用原始的score1/2/3变量做判断,完全没关联用户的答题结果。
  • 最终分数的多分支判断冗余且逻辑错误,比如score1 and score2 == True的写法不符合Python逻辑运算优先级,且逻辑本身不成立。

修正后的代码

# 定义题目、答案和对应分数(分数改用整数类型)
questions = [
    ('When python was created?: ', '1991', 10),
    ('Who wrote python?: ', 'Guido van Rossum', 20),
    ('How we call an anonymous functions in python?: ', 'lambda', 30)
]

total_score = 0

# 遍历题目并处理答题
for question, correct_answer, points in questions:
    print(question)
    user_answer = input('Enter your answer: ')
    print()
    if user_answer == correct_answer:
        print('Correct')
        total_score += points
        print(f'Current score: {total_score}')
    else:
        print('False')
        print(f'Current score: {total_score}')
    print()

# 输出最终总分
print(f'Your final score is: {total_score}')

关键修改点

  • 将题目、答案、分数整合为列表元组,简化代码结构,方便后续扩展更多题目。
  • 初始化total_score变量,答对一题就累加对应分数,逻辑清晰直接。
  • 分数改用整数类型,确保数值运算正确。
  • 移除冗余的result变量和错误的多分支判断,直接输出累加后的总分。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 14:01:40