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

Python文本游戏仅能拾取3个物品,其余拾取提示无效求排查

问题分析与修复

问题原因

你的游戏仅能拾取三个房间物品的核心问题,出在拾取物品的判断逻辑上:

  1. 匹配逻辑错误:原代码用' '.join(player_move[1:]) in rooms[current_room]['item']判断输入是否正确,这是检查输入物品名是否包含于房间物品名,而非完全匹配。单字物品(如Chalice、Key)刚好能满足,但多字物品(如Shadow cloak、Glowing orb)会因为输入处理后的大小写差异(输入经title()处理后每个单词首字母大写,和房间内物品名的大小写不一致)导致匹配失败。
  2. 未前置检查房间物品:原逻辑没先判断当前房间是否存在物品,若玩家在无物品房间输入get,会直接触发潜在的KeyError。

修复方案

修改主循环中的拾取判断逻辑,改成完全匹配+大小写不敏感的模式,同时先校验房间是否有物品:

关键修复代码

# 替换原拾取逻辑部分
elif player_move[0] == 'Get':
    # 统一转为小写,消除大小写差异影响
    input_item = ' '.join(player_move[1:]).lower()
    if 'item' in rooms[current_room]:
        room_item = rooms[current_room]['item'].lower()
        if input_item == room_item:
            print('You picked up the {}'.format(rooms[current_room]['item']))
            print('-----')
            get_item(current_room, rooms, items_collected)
        else:
            print('Invalid move, try again')
    else:
        print('Invalid move, try again')

另外,原移动命令的判断存在漏洞(未检查第一个词是否为Go,输入abc East也会触发移动),建议补上校验:

# 替换原移动逻辑部分
if len(player_move) >= 2 and player_move[0] == 'Go' and player_move[1] in rooms[current_room].keys():
    current_room = moving_rooms(current_room, player_move[1], rooms)

完整修复后的主循环代码

while True:
    # 胜利/失败判断
    if current_room == 'Enigmatic Vault':
        if len(items_collected) == 6:
            print('Congratulations, you have collected all items and defeated the villain')
            print('Thanks for playing!')
            break
        else:
            print('You have encountered the shadow lord but you did not collect all 6 items to defeat him,'
                  'he has turned the whole world to eternal night and has defeated you')
            print('Thanks for playing!')
            break

    print("You are in the ", current_room)

    # 显示背包状态
    if not items_collected:
        print("You don't have any items.")
    else:
        print('You have:', ', '.join(items_collected))

    # 显示当前房间物品
    if current_room != 'Enigmatic Vault' and 'item' in rooms[current_room].keys():
        print('You found the {}'.format(rooms[current_room]['item']))
    print('-----')
    
    player_move = input('Enter your move: ').title().split()

    # 移动逻辑
    if len(player_move) >= 2 and player_move[0] == 'Go' and player_move[1] in rooms[current_room].keys():
        current_room = moving_rooms(current_room, player_move[1], rooms)

    # 拾取逻辑
    elif player_move[0] == 'Get':
        input_item = ' '.join(player_move[1:]).lower()
        if 'item' in rooms[current_room]:
            room_item = rooms[current_room]['item'].lower()
            if input_item == room_item:
                print('You picked up the {}'.format(rooms[current_room]['item']))
                print('-----')
                get_item(current_room, rooms, items_collected)
            else:
                print('Invalid move, try again')
        else:
            print('Invalid move, try again')

    # 退出逻辑
    elif player_move == ['Exit']:
        print('You are exiting the game')
        print("Thanks for playing!")
        break
    
    # 无效输入处理
    else:
        print('Invalid move, try again')

额外优化建议

可以把错误提示细化,比如当玩家拾取不存在的物品时,提示"This room doesn't have that item!",比统一的"Invalid move"更直观。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 06:55:13