在Python中退出if/elif语句块

编程语言 2026-07-09

我现在在写一个文本冒险风格的游戏,想练习一些if/elif语句。现在遇到了一个关于如何退出if/elif结构的问题。我想让它从最初的if/elif语句继续执行,也就是从最初的elif语句所在的位置往后继续(代码的最后两行)。有可能做到吗?这是我目前的思路:

cave_or_jungle = input("Do you go into the JUNGLE or the CAVE?").lower()

if cave_or_jungle == "cave":
    print("You go into the cave! The cave is cold and dark. You find a rusty lantern and hear strange growling deeper inside.")
    deeper_or_leave = input("Do you go DEEPER or LEAVE?").lower()

    if deeper_or_leave == "deeper":
        print("Deep in the cave, a giant beast jumps out of the darkness.")
        fight_or_run = input("Do you FIGHT or RUN?").lower()

        if fight_or_run == "fight":
            print("The beast is too powerful. You are defeated.")
            print("GAME OVER")
            # Game ends
        elif fight_or_run == "run":
            print("You run back outside and follow the jungle path.")
            # Exit statement here to continue onto line 47 below. Code now runs up to this statement and then stops.
    elif deeper_or_leave == "leave":
        print("You go back outside.")
        # Exit statement here to continue onto line 47 below. Code now runs up to this statement and then stops.
elif cave_or_jungle == "jungle":
    print("You are in the jungle!")
    print("The jungle is thick and noisy. You hear water nearby and see smoke rising through the trees.")
    input("Do you follow the RIVER or the SMOKE?")
    # Have previous two elif statements run to this elif statement and progress with story.

解决方案

你可以用一个循环来解决这个问题:只要游戏还没结束,就检查当前所在的位置并执行相应的分支。break 终止循环,而 continue “重新开始” 循环:

cave_or_jungle = input("Do you go into the JUNGLE or the CAVE?").lower()

while True:
    if cave_or_jungle == "cave":
        print("You go into the cave! The cave is cold and dark. You find a rusty lantern and hear strange growling deeper inside.")
        deeper_or_leave = input("Do you go DEEPER or LEAVE?").lower()

        if deeper_or_leave == "deeper":
            print("Deep in the cave, a giant beast jumps out of the darkness.")
            fight_or_run = input("Do you FIGHT or RUN?").lower()

            if fight_or_run == "fight":
                print("The beast is too powerful. You are defeated.")
                print("GAME OVER")
                # Game ends
                break
            elif fight_or_run == "run":
                print("You run back outside and follow the jungle path.")
                cave_or_jungle = "jungle"
                # skip rest of loop and start from top
                continue
        elif deeper_or_leave == "leave":
            print("You go back outside.")
            cave_or_jungle = "jungle"
            # skip rest of loop and start from top
            continue
    elif cave_or_jungle == "jungle":
        print("You are in the jungle!")
        print("The jungle is thick and noisy. You hear water nearby and see  smoke rising through the trees.")
        input("Do you follow the RIVER or the SMOKE?")
        # Have previous two elif statements run to this elif statement and progress with story.

在Python 3.10及更高版本,你也许可以考虑使用 match 语句。它对循环本身不会有任何影响,但会让重复的条件判断稍微简单一些:

cave_or_jungle = input("Do you go into the JUNGLE or the CAVE?").lower()

while True:
  match cave_or_jungle:
    case "cave":
      print("You go into the cave! The cave is cold and dark. You find a rusty lantern and hear strange growling deeper inside.")
      deeper_or_leave = input("Do you go DEEPER or LEAVE?").lower()

      match deeper_or_leave:
        case "deeper":
          print("Deep in the cave, a giant beast jumps out of the darkness.")
          fight_or_run = input("Do you FIGHT or RUN?").lower()

          match fight_or_run:
            case "fight":
              print("The beast is too powerful. You are defeated.")
              print("GAME OVER")
              # Game ends
              break
            case "run":
              print("You run back outside and follow the jungle path.")
              cave_or_jungle = "jungle"
              # skip rest of loop and start from top
              continue
        case "leave":
          print("You go back outside.")
          cave_or_jungle = "jungle"
          # skip rest of loop and start from top
          continue
    case "jungle":
      print("You are in the jungle!")
      print("The jungle is thick and noisy. You hear water nearby and see  smoke rising through the trees.")
      input("Do you follow the RIVER or the SMOKE?")
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章