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

Python初学者求助:解决'str'与'int'实例不支持'>'比较的报错问题

问题根因梳理

  • 类型不兼容报错:用户按回车选择连续出题模式时,check_how_many_questions函数返回空字符串"",原代码第149行直接将字符串类型的user_choice_of_questions和整数类型的已答题数做>比较,触发类型错误。
  • 重开游戏计数不重置:已答题数、答对题数、答错题数三个计数变量定义在全局区域,没有放在外层游戏循环内部,每次新开游戏不会清空上一轮的旧数据。

修复方案

修改点说明

  1. 调整出题循环的判断逻辑,连续模式下不需要做大小比较,直接进入循环即可
  2. 将所有局内计数变量挪到外层游戏循环内部,确保每轮新游戏自动重置为0
  3. 补充中途主动退出游戏后的计数重置逻辑,避免残留数据影响下一轮

修复后完整代码

import random

# 校验用户选择的答题数量
def check_how_many_questions(question):
    while True:
        response = input(question)
        how_many_questions_error = "请输入大于0的整数,或按<回车>选择连续出题模式"
        if response != "":
            try:
                response = int(response)
                if response < 1:
                    print(how_many_questions_error)
                    continue
            except ValueError:
                print(how_many_questions_error)
                continue
        return response

# 校验游戏模式输入
def game_mode_input_checker(question):
    while True:
        error_message = "输入错误,请输入1-4之间的整数(1.加法 2.减法 3.乘法 4.除法)"
        try:
            response = float(input(question))
            if response == 1:
                return response
            else:
                print(error_message)
                print()
                continue
        except ValueError:
            print(error_message)
            print()

# 校验答题输入
def input_checker(question):
    while True:
        try:
            response = float(input(question))
            return response
        except ValueError:
            print("<错误> 请输入整数\n")
            continue

# 校验继续/退出输入
def continue_game(question):
    valid = False
    while not valid:
        response = input(question).lower()
        if response == "":
            return response
        elif response == "xxx":
            return response
        else:
            print()
            print("<错误> 请按<回车>继续,或输入'xxx'退出")
            print()

# 标题装饰生成器
def statement_generator(statement, decoration):
    sides = decoration * 3
    statement = "{} {} {}".format(sides, statement, sides)
    top_bottom = decoration * len(statement)
    print(top_bottom)
    print(statement)
    print(top_bottom)
    return ""

# 主程序
game_loop = ""
while game_loop == "":
    # 计数变量移到此处,每轮新游戏自动重置
    questions_answered_correct = 0
    questions_answered_incorrect = 0
    number_of_questions_answered = 0

    user_choice_of_questions = check_how_many_questions("你可以选择两种答题模式:\n1. 固定题数:输入大于0的整数即可\n2. 连续出题:直接按<回车>\n\n请选择答题数量:")

    game_mode = game_mode_input_checker("请选择游戏模式(1.加法):")

    # 修改循环判断条件,兼容连续模式
    while user_choice_of_questions == "" or user_choice_of_questions > number_of_questions_answered:

        if user_choice_of_questions == "":
            heading = "连续模式:第{}题".format(number_of_questions_answered + 1)
        else:
            heading = "第{}题 / 共{}题".format(number_of_questions_answered + 1, user_choice_of_questions)

        if game_mode == 1:
            statement_generator(heading, "#")
            number_1 = random.randint(0,20)
            number_2 = random.randint(0,20)
            answer = number_1 + number_2
            response = input_checker("{} + {} = ?  ".format(number_1, number_2))

            if response == answer:
                result = "答对了"
                question_outcome = "第{}题 | 结果:{} | 你的答案:{} | 正确答案:{}".format(number_of_questions_answered + 1, result, response, answer)
                print()
                number_of_questions_answered += 1
                questions_answered_correct += 1
            else:
                result = "答错了"
                question_outcome = "第{}题 | 结果:{} | 你的答案:{} | 正确答案:{}".format(number_of_questions_answered + 1, result, response, answer)
                print()
                number_of_questions_answered += 1
                questions_answered_incorrect += 1

            print(question_outcome)

            if number_of_questions_answered == user_choice_of_questions:
                number_of_questions_answered = 0
                break

            if number_of_questions_answered >= 1:
                print()
                game_loop = continue_game("按<回车>继续答题,输入'xxx'退出当前游戏:")
                print()

            if game_loop == "xxx":
                # 中途退出重置计数
                number_of_questions_answered = 0
                break

    game_loop = continue_game("按<回车>重开游戏,输入'xxx'彻底退出:")
    print()

print("感谢游玩Quiz Quest答题游戏")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 19:15:01