Python文本游戏if-else逻辑异常:输入get命令误触发move分支问题
Python文字冒险游戏问题修复方案
核心错误排查
你遇到的指令分支触发错误是因为条件语句的逻辑写法不符合Python规则:
你写的if tokens[0] == 'move' or 'Move'中,or运算符左右是独立的布尔判断项,非空字符串'Move'的布尔值永远为True,因此这个条件无论输入什么都会判定成立,永远走移动分支,不会触发拾取逻辑。
正确的条件写法可以二选一:
- 完整写两个判断:
if tokens[0] == 'move' or tokens[0] == 'Move' - 更简洁的大小写兼容写法:
if tokens[0].lower() == 'move',get的条件判断同理修改即可。
其他需要修复的功能问题
- 房间字典中
Laboratory的道具key是大写Item,和其他房间统一的小写item不一致,会导致读取该房间道具时报错,改为小写item即可 show_instructions函数没有返回值,调用时直接写show_instructions()即可,不要套print(),否则会额外打印无用的None- 输入单个单词(比如无参数的move/get)时,
tokens[1]会触发索引越界报错,需要补充输入长度校验 - 拾取道具后没有清空房间道具,会导致可以重复拾取同一个道具,拾取成功后可删除该房间的
item字段或置空 - 原代码存在缩进错误,Python对缩进敏感,需要补全所有函数内代码的缩进
修复后完整可运行代码
def show_instructions(): print('Collect 8 items to win the game and defeat the beast') print('Move Commands: move North, move South, move West, move East, or Exit to leave the game') print('Pickup items: get item, ex. get sword\n') def main(): # 房间与道具字典 rooms = { 'Shed' : {'West': 'Well Room', 'item' : 'well room West of you'}, 'Well Room' : {'South' : 'Sewers', 'East' : 'Shed', 'item' : 'Healing-Potion'}, 'Sewers' : {'East' : 'Crawl Space','North' :'Well Room', 'item' : 'Armor'}, 'Crawl Space' : {'East' : 'Cavern', 'West' : 'Sewers','item' : 'Sword'}, 'Cavern' : {'South' : 'Dungeon Hall', 'West' : 'Crawl Space', 'item' : 'Shield'}, 'Dungeon Hall' : {'East' : 'Hidden Room', 'West' : 'War Room', 'North' : 'Cavern', 'item' : 'Sword-Enchantment'}, 'Hidden Room' : {'South' : 'Treasure Room', 'East' : 'Dungeon Hall', 'item' : 'Flame-Resistance-Potion'}, 'War Room' : {'West' : 'Laboratory', 'East' : 'Dungeon Hall', 'item' : 'Armor-Enchantment'}, 'Laboratory' : {'South' : 'Treasure Room', 'East' : 'War Room', 'item' : 'Enchantment-Table'}, 'Treasure Room' : {'item' : 'Dragon'} } directions = ['North','South','East','West'] currentroom = 'Shed' inventory = [] show_instructions() while currentroom != 'Exit': # 显示当前状态 print('---------------------------') print('You are in the', currentroom) print('Inventory:', inventory) if 'item' in rooms[currentroom]: print('You see a', rooms[currentroom]['item']) print('---------------------------') # 胜利/失败判定 if currentroom == 'Treasure Room': if len(inventory) < 8: print('You get eaten by the Dragon! Game Over.') break else: print('You slay the dragon and return to your boss victorious! You Win!') break # 读取用户输入 c = input('What would you like to do?:\n').strip() # 退出逻辑 if c == 'Exit': currentroom = 'Exit' continue tokens = c.split() # 输入合法性校验 if len(tokens) < 2: print('Not a Valid Entry, please follow the command format') continue # 移动分支 if tokens[0].lower() == 'move': if tokens[1] in directions: if tokens[1] in rooms[currentroom]: currentroom = rooms[currentroom][tokens[1]] else: print('You see a Wall') else: print('Not a Valid Direction') # 拾取分支 elif tokens[0].lower() == 'get': target_item = ' '.join(tokens[1:]) if 'item' in rooms[currentroom] and target_item == rooms[currentroom]['item']: inventory.append(target_item) print(f'You got {target_item}!') del rooms[currentroom]['item'] # 拾取后移除房间内道具 else: print('Not a valid Item') else: print('Not a Valid Entry') if __name__ == "__main__": main()
内容的提问来源于stack exchange,提问作者Coltin W
相关产品推荐
相关产品推荐

