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

Python文字冒险游戏:连续if查询在while循环中的问题

解决文字冒险游戏的问题输入循环与流程优化

你的核心问题是每个问题输入无效时,没有单独的循环逻辑,导致输入错误后直接回到第一个问题。下面给出几种高效的实现方式:

一、修复原逻辑:给每个问题添加独立循环

给每个问题单独套一层while True,确保输入无效时只重复当前问题,而非回到流程起点:

# 第一个问题
while True:
    question1 = input("question1: ")
    if question1 == "1":
        print("Wrong")
        break
    elif question1 == "2":
        print("Right, go on!!")
        break
    else:
        print("Use a valid answer!")

# 仅答对第一个问题才进入第二个
if question1 == "2":
    while True:
        question2 = input("question2: ")
        if question2 == "1":
            print("Wrong")
            break
        elif question2 == "2":
            print("Right, go on!!")
            break
        else:
            print("Use a valid answer!")

# 仅答对前两个才进入第三个
if question1 == "2" and question2 == "2":
    while True:
        question3 = input("question3: ")
        if question3 == "1":
            print("Wrong")
            break
        elif question3 == "2":
            print("Right, go on!!")
            break
        else:
            print("Use a valid answer!")

二、更高效的方式:封装提问函数

把“提问→验证输入→返回有效答案”的逻辑封装成函数,避免重复代码,让流程更清晰:

def ask_valid_question(prompt):
    """持续提问直到用户输入1或2,返回有效答案"""
    while True:
        answer = input(prompt)
        if answer in ("1", "2"):
            return answer
        print("Use a valid answer!")

# 游戏流程
answer1 = ask_valid_question("question1: ")
if answer1 == "1":
    print("Wrong")
else:
    print("Right, go on!!")
    answer2 = ask_valid_question("question2: ")
    if answer2 == "1":
        print("Wrong")
    else:
        print("Right, go on!!")
        answer3 = ask_valid_question("question3: ")
        if answer3 == "1":
            print("Wrong")
        else:
            print("Right, go on!!")

三、用Python的match-case简化分支判断(Python3.10+)

如果你的Python版本在3.10及以上,可以用match-case(即你听说的switch/case)替代嵌套if,让分支逻辑更直观:

def ask_valid_question(prompt):
    while True:
        answer = input(prompt)
        if answer in ("1", "2"):
            return answer
        print("Use a valid answer!")

answer1 = ask_valid_question("question1: ")
match answer1:
    case "1":
        print("Wrong")
    case "2":
        print("Right, go on!!")
        answer2 = ask_valid_question("question2: ")
        match answer2:
            case "1":
                print("Wrong")
            case "2":
                print("Right, go on!!")
                answer3 = ask_valid_question("question3: ")
                match answer3:
                    case "1":
                        print("Wrong")
                    case "2":
                        print("Right, go on!!")

原代码问题分析

你之前的代码里,第二个、第三个问题没有独立的循环:当输入非1/2时,代码会直接跳出当前的if分支,回到最外层的while True,所以会重新开始第一个问题。而如果盲目加continue或额外while True,因为没有正确的break条件,就会触发无限循环。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 03:55:12