You need to enable JavaScript to run this app. Python文本类游戏项目运行触发Break Room相关KeyError报错求助
优惠活动
大模型
产品
解决方案
定价
更多

Python文本类游戏项目运行触发Break Room相关KeyError报错求助

问题排查结果

核心错误原因

你遇到的KeyError是因为房间名称大小写拼写不一致:

  • rooms字典中Main Storage的North方向指向的房间名为Break room(单词room的首字母r小写)
  • 但rooms的顶层房间key、items字典的房间key均为Break Room(单词room的首字母R大写)
    当你从Main Storage向北移动后,state变量被赋值为Break room,用这个值去items字典查询内容时,没有匹配的key就触发了报错。

修复方法

快速修复(1行修改)

直接调整rooms字典中Main Storage的北向指向值,和全局统一拼写即可:

rooms = {
    'Meeting Room': {'North': 'Main Storage', 'East': 'Kitchen'},
    # 把Break room改成Break Room即可
    'Main Storage': {'North': 'Break Room', 'East': 'Janitorial Closet', 'South': 'Meeting Room', 'West': 'Generator Room'},
    'Generator Room': {'East': 'Main Storage', 'North': 'Storage Closet'},
    'Break Room': {'South': 'Main Storage', 'West': 'Dirty Bathroom'},
    'Dirty Bathroom': {'East': 'Break Room'},
    'Storage Closet': {'South': 'Generator Room'},
    'Janitorial Closet': {'West': 'Main Storage'}
}

额外优化建议

你当前写的get_new_state函数不需要遍历整个rooms字典,字典本身支持直接通过键查询,修改后运行效率更高:

def get_new_state(state, direction):
    if state in rooms and direction in rooms[state]:
        return rooms[state][direction]
    return state

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 14:36:04