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

Python游戏Bug排查:living_room()无法启动及房间切换失效

问题修复方案

核心问题分析

  1. 默认启动房间错误:start()函数里初始Location被设为"attic",导致默认进入阁楼。
  2. 房间切换无效:每个房间函数内的Location、Inventory、Gamewon等都是局部变量,修改后不会同步到start()的全局状态,导致切换后状态不更新。
  3. 其他隐性问题:living_room()的循环条件大小写不匹配("Living room" vs "living room"),且每次循环重置Inventory导致物品丢失;跨房间状态(如Mouse、Hole)无法共享。

修改后的完整代码

def living_room(Inventory, Hole, Mouse):
    Gamewon = False
    Location = "living room"
    while Location == "living room":
        print("")
        print("Room: living room")
        print("You are in the living room of the house. You see in this Room:")
        print("A pot of soil")
        print("Stairs going to the second floor")
        print("A dark doorway")
        print("A ball of string")
        print("")
        print("Room actions:")
        print("(l)ook: go closer to look at the pot of soil") 
        if Inventory == "" or Inventory == "Cheese":
            print("(p)ick up: ball of string")    
        print("(g)o: through the dark doorway")
        print("(u)p: take the stairs to the second floor")
        selection = input("Your selection: ").lower()     
        if selection == "l":
            if Mouse == "full":
                print("The growing beanstalk takes you away to safety!")
                Gamewon = True
                Location = "exit"
            else:
                print("The soil looks very dry")
        elif Inventory == "" and selection == "p":
            Inventory = "String"
            print("You have picked up the string")
        elif selection == "g":
            Location = "bed room"
        elif selection == "u":
            print("You take the stairs to the attic")
            Location = "attic"
        else:
            print("Please make a proper selection")
    return Location, Inventory, Hole, Mouse, Gamewon

def attic(Inventory, Hole, Mouse):
    Gamewon = False
    Location = "attic"
    while Location == "attic":
        print("")
        print("Room: Attic")
        print("You have entered the attic of the house. In this room there is:")
        print("A large assortment of random cheeses being stored up here.")
        print("A hole much smaller than the pieces of cheese.")
        print("")
        print("Room actions:")
        print("(c)heese: Pick up cheese and attempt to drop it in the hole.")
        if Inventory == "String" or Inventory == "String,Cheese":
            print("(s)tring: Drop the string into the hole.")
        print("(p)ick up: Pick up a piece of cheese and keep it.")
        print("(d)own: Go down the stairs.")
        selection = input("Your selection: ").lower()
        if selection == "c":
            print("The cheese does not fit down this hole.")
        elif selection == "s":
            print("The string has disappeared down the hole.")
            Hole = "DroppedString"
        elif selection == "p":
            print("You have picked up a piece of cheese.")
            if Inventory == "String":
                Inventory = "String,Cheese"
            else:
                Inventory = "Cheese"
        elif selection == "d":
            print("You have gone down the stairs.")
            Location = "living room"
        else:
            print("Please make a proper selection")
    return Location, Inventory, Hole, Mouse, Gamewon

def bed_room(Inventory, Hole, Mouse):
    Gamewon = False
    Location = "bed room"
    while Location == "bed room":
        print("")
        print("Room: Bedroom")
        print("You have entered the bedroom.")
        if Hole == "":
            print("In the room there is the usual furnishings along with a cat staring at a mouse hole.")
        if Hole == "DroppedString":
            print("In the room there is the usual furnishings along with a mouse in front of the mouse hole.")
        print("")
        print("Room actions:")
        print("(g)o through the entrance way.")
        if Inventory == "String":
            print("(u)se ball of string to play with cat.")
        elif Hole == "DroppedString" and ("Cheese" in Inventory):
            print("(f)eed the mouse the cheese")
        selection = input("Your selection: ").lower()
        if selection == "g":
            print("You have left the bedroom.")
            Location = "living room"
        elif selection == "u":
            print("The cat looks at the string but then goes back to the mouse hole.")
        elif selection == "f" and ("Cheese" in Inventory) and Hole == "DroppedString":
            print("The mouse happily eats the cheese and scurries out of the room.")
            Mouse = "full"
            # 吃掉奶酪后移除物品
            if Inventory == "Cheese":
                Inventory = ""
            elif Inventory == "String,Cheese":
                Inventory = "String"
        else:
            print("Please make a proper selection")
    return Location, Inventory, Hole, Mouse, Gamewon

def start():
    # 初始化全局状态:位置、物品、洞的状态、老鼠状态、游戏是否胜利
    Location = "living room"
    Inventory = ""
    Hole = ""
    Mouse = ""
    Gamewon = False
    while not Gamewon:
        if Location == "attic":
            Location, Inventory, Hole, Mouse, Gamewon = attic(Inventory, Hole, Mouse)
        elif Location == "living room":
            Location, Inventory, Hole, Mouse, Gamewon = living_room(Inventory, Hole, Mouse)
        elif Location == "bed room":
            Location, Inventory, Hole, Mouse, Gamewon = bed_room(Inventory, Hole, Mouse)
    print("Congratulations you have won the game!")

start()

关键修改点说明

  • 默认启动房间修正:start()里初始Location改为"living room",符合需求。
  • 状态变量共享:每个房间函数接收Inventory、Hole、Mouse作为参数,执行后返回更新后的状态(包括新位置和游戏胜利状态),确保start()能同步状态。
  • 循环条件修复:living_room()的循环条件改为Location == "living room"(统一小写,匹配变量值),确保循环正常执行。
  • 物品状态保留:移除房间函数内的Inventory = ""重置代码,捡起的物品会持续保留。
  • 输入处理优化:将用户输入转为小写(.lower()),避免大小写输入导致的识别错误。
  • 逻辑补全:修复了奶酪与字符串共存时的物品状态处理,以及喂老鼠后的物品移除逻辑,让游戏流程更合理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 02:45:43