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

Python CaveGame通关首模块后无法续进的缩进问题求助

CaveGame V.4.1 流程异常修复方案

问题根源

你的代码存在缩进逻辑错误:PART2至PART6的所有代码都嵌套在PART1的elif guess != tnl1:分支内部。这导致只有玩家在PART1失败(遭遇drakon)时才会执行后续模块,而猜对隧道后,程序执行完if guess == tnl1:分支的内容就直接终止,无法进入后续流程。

修复方案

将PART2至PART6的代码整体移出PART1的if/elif结构,改为平级的顺序执行逻辑;同时修正内部的嵌套缩进,确保每个模块无论当前选择结果如何,都会推进到下一个模块。

修复后的完整代码

#CAVEGAME
#Import
import random
import pickle
#Introduction
print("--------CaveGame V.4.1----------")
print("Credits - Rhys")
#Name loading
account_name = pickle.load(open( "caveuser1.pickle", "rb" ))
if account_name == 'Rhys':
    print("Welcome Rhys. Dev mode active")
elif account_name == 'Dev':
    print("Welcome developer. Dev mode active")
elif account_name == 'Mannat':
    print("You are not authorised to access this game.")
    quit()
elif account_name == 'Tammana':
    print("You are not authorised to access this game.")
    quit()
elif account_name == 'Yameen':
    print("You are not authorised to access this game - YAMEEN. ")
    quit()
elif account_name == 'CroissantMafia':
    print("Welcome Croissant Disciples.")
else:
    print("Welcome to CaveGame")
#Stats
health = 20
damage = 1.5
points = 0
#PART1
print(account_name, "you are travelling down a cave and come to a fork in the cave. There are two paths - which do you choose?")
guess = int(input("Make a guess"))
tnl1 = random.randint(1,2)
if guess == tnl1:
    print('You survived')
    points = points + 50
else:
    print("You found a drakon")
    drakon1_health1 = 6
    drakon1_health2 = drakon1_health1 - damage
    drakon1_damage = 0.5
    health = health - drakon1_damage
    print("Your health is",health, "and the drakon's health is", drakon1_health2)
    health = health - drakon1_damage
    print("The drakon is defeated and your health is", health)
    points = points - 50

#PART2
print("Well", account_name, "you are travelling down a cave and come to another fork in the cave. There are two paths - which do you choose?")
guess2 = int(input("Make a guess"))
tnl2 = random.randint(1,2)
if guess2 == tnl2:
    print('You survived')
    print("You recieved a combat upgrade. You now deal more damage")
    points = points + 50
    damage = damage + 2
else:
    print("A wave of fire rolls through the tunnel. You lose 5 health")
    health = health - 5
    points = points - 50

#PART3
print("Well", account_name, "you are travelling down a cave and come to another fork in the cave. This time there are three paths - which do you choose?")
guess3 = int(input("Make a guess"))
tnl3 = random.randint(1,3)
if guess3 == tnl3:
    print('You survived')
    print("You recieved a health upgrade.")
    health = health + 1.4
    points = points + 50
else:
    print("There was a cave in. You lost")
    points = points - 50
    # 若需玩家在此直接结束游戏可保留quit(),否则移除即可继续后续流程
    # quit()

#PART4
print("Well", account_name, "you are travelling down a cave and come to another fork in the cave. This time there are three paths - which do you choose?")
guess4 = int(input("Make a guess"))
tnl4 = random.randint(1,3)
if guess4 == tnl4:
    print('You survived')
    points = points + 50
else:
    print("You found Cleo. Your damage has been increased but at a cost of some health")
    health = health - 2
    damage = damage + 1.4
    points = points - 50

#PART5
print("Well", account_name, "you are travelling down a cave and come to another fork in the cave. This time there are three paths - which do you choose?")
guess5 = int(input("Make a guess"))
tnl5 = random.randint(1,3)
if guess5 == tnl5:
    print('You survived')
    print("You got 50 extra points!")
    points = points + 100
else:
    print("You encounter a demigorgon")
    demigorgon1_health1 = 8
    demigorgon1_damage = 2.5
    demigorgon1_health2 = demigorgon1_health1 - damage
    health = health - demigorgon1_damage
    print("Your health is", health, "and the demigorgon's is", demigorgon1_health2)
    demigorgon1_health3 = 0
    health = health - demigorgon1_damage
    print("The demigorgon is defeated but your health is", health)
    points = points - 50

#PART6
print("Well", account_name, "you are travelling down a cave and come to another fork in the cave. This time there are three paths - which do you choose?")
guess6 = int(input("Make a guess"))
tnl6 = random.randint(1,3)
if guess6 == tnl6:
    print('You survived')
    points = points + 50
    print("These are your final points:", points," and this is your final health:", health)
else:
    print("You fell into a pool of acid. You lost health and extra points")
    health = health - 4
    points = points - 100
    print("These are your final points:", points," and this is your final health:", health)

关键修改说明

  1. 把PART2-PART6的代码全部移出PART1的分支结构,改为顺序执行,确保无论PART1结果如何都会进入后续模块
  2. 将所有elif guess != xxx改为else,逻辑更简洁清晰
  3. 修正了PART3中quit()的位置(可根据需求决定是否保留)
  4. 统一了代码缩进,避免嵌套错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 07:30:50