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

Python使用多重while循环开发答题游戏出现重开失效、死循环问题求助

问题根因
  • 核心状态变量未每轮重置:lives、points、repeat仅在程序启动时初始化一次,首次游戏结束后lives变为0,重玩时没有重置该变量,导致选完主题后直接跳过答题流程,看起来无法正常选择主题。
  • 错误答题分支未修改repeat标记:仅答对时会将repeat设为False,答错时repeat保持为True,内层while repeat循环会反复将index重置为0,触发答案输入环节的无限循环。
  • 标记位职责混淆:tryAgain同时承担了「主题选择错误重试」和「游戏全局重玩」两个功能,导致主题选择正确后仍然会重复打印主题列表。
  • 分支逻辑缺失:当前仅实现了主题1的答题逻辑,选择主题2、3时没有对应处理代码,也会直接跳到重玩询问,和「无法选择主题」的表现一致。
  • 退出逻辑不完整:用户选择不重玩时没有将tryAgain设为False,程序无法正常退出;答题结束后的break仅能跳出最内层输入循环,无法结束上层答题流程。
修复方案
  1. 每轮游戏开始前重置所有状态变量,包括lives、points、repeat
  2. 答错分支中添加repeat = False,避免内层循环卡死
  3. 拆分标记位职责,单独用变量控制主题选择的重试逻辑
  4. 补全主题2、3的对应逻辑,添加游戏结束后的上层循环跳出逻辑
  5. 补充不重玩时的退出逻辑,优化冗余的if-elif计分为字典映射,减少重复代码
修复后代码示例
# 全局常量定义,可自行调整初始生命值
INIT_LIVES = 3  

playGame = True
while playGame:
    # 每轮游戏开始前重置所有状态
    tryAgain = True
    lives = INIT_LIVES
    points = 0
    answer = []
    point_map = {"ans1":10,"ans2":20,"ans3":30,"ans4":40,"ans5":50,
                 "ans6":60,"ans7":70,"ans8":80,"ans9":90,"ans10":100}
    
    # 主题选择逻辑
    while tryAgain:
        print("TOPICS: \n")
        print("1. topic 1")
        print("2. topic 2")
        print("3. topic 3")
        print("======================================")
        num = int(input("PICK A NUMBER FOR THE TOPIC [1-3]: "))
        if num > 3 or num <1:
            print("There is no given topic")
            print("==========================")
            tryAgain = True
        else:
            tryAgain = False
    
    # 答题逻辑
    if num == 1: 
        # 保留原有文件读取逻辑
        # reader = WordListCorpusReader('filename', ['textfile'])
        with open('filename') as f:
            text = [line.strip().upper() for line in f]
        gameOver = False
        while lives > 0 and not gameOver:
            repeat = True
            while repeat and not gameOver:
                index = 0
                while index < 10 and not gameOver:
                    answerlst = input(str(index + 1) + '. ').upper()
                    if answerlst in text:
                        if answerlst in answer:
                            print("\nYou repeated your answer!")
                            repeat = True
                            continue
                        else:
                            answer.append(answerlst)
                            print("Correct!")
                            repeat = False
                            add_point = point_map.get(answerlst,0)
                            points += add_point
                            print(f"Points: {add_point}")
                            if points == 550:
                                print("\nCONGRATULATIONS!!!! YOU GOT A PERFECT POINTS OF:", points)
                                gameOver = True
                                break
                    else:
                        add_point = point_map.get(answerlst,0)
                        points += add_point
                        lives -= 1
                        repeat = False # 修复:错误时关闭重复标记
                        if lives == 0:
                            print("\nGAME OVER!!!!!\n")
                            print("You just got a: ", points)
                            gameOver = True
                            break
                    index +=1
    # 按需补全其他主题逻辑
    elif num ==2:
        pass
    elif num ==3:
        pass

    # 重玩询问
    playAgain = input("Do you want to play again? [Y] / [N]: ").upper()
    if playAgain != 'Y':
        playGame = False

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 09:09:01