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

访问字典键值对后如何删除?文本游戏重复拾取问题求解

问题解决:实现物品拾取后无法重复获取的功能

你的核心问题是执行Acquire命令时未移除房间内的物品,且原代码存在逻辑分支错误(成功执行命令后仍会触发"Invalid Move"提示)。以下是修复后的完整代码,同时解决了重复拾取的问题:

def show_instructions():
    print('Welcome to Rocket Escape!\n')
    print('Collect the 6 spaceship parts to escape the planet before the evil Corporate Mining Company catches you!')
    print('Movement Commands: North, South, East, West')
    print('To pickup an item and add it to inventory, type "Acquire"')
    print('To exit the game, type: Exit')


rooms = {
    'House': {'North': 'Fields', 'East': 'Library'},
    'Library': {'West': 'House', 'item': 'Map'},
    'Fields': {'North': 'Fuel Depot', 'East': 'Lab', 'South': 'House', 'West': 'Launch Pad', 'item': 'Rations'},
    'Launch Pad': {'East': 'Fields', 'item': 'Keys'},
    'Fuel Depot': {'South': 'Fields', 'East': 'Front Gate', 'item': 'Fuel'},
    'Lab': {'North': 'Shed', 'West': 'Fields', 'item': 'Spacesuit'},
    'Shed': {'South': 'Lab', 'item': 'Ammunition'},
    'Front Gate': {'West': 'Fuel Depot', 'item': 'Corporate Mining Company!'}}


def player_stat():
    print('')
    print('You are in the {}'.format(current_room))
    print('Current Inventory: {}'.format(current_inv))
    if 'item' in rooms[current_room]:
        print('\nYou see {}!'.format(rooms[current_room]['item']))
    print('')


current_inv = []
current_room = 'House'
player_move = ''

show_instructions()

while True:
    player_stat()
    player_move = input('Enter your move:\n').title()
    invalid_move = True  # 标记是否为无效命令
    if player_move == 'Exit':
        print('Exiting. Thanks for playing!')
        break
    elif player_move in rooms[current_room]:
        current_room = rooms[current_room][player_move]
        invalid_move = False
    elif player_move == 'Acquire':
        # 检查当前房间是否有可拾取的物品(排除反派房间)
        if 'item' in rooms[current_room] and rooms[current_room]['item'] != 'Corporate Mining Company!':
            item = rooms[current_room]['item']
            current_inv.append(item)
            print('You acquired {}!'.format(item))
            # 删除房间内的物品键值对,防止重复拾取
            del rooms[current_room]['item']
        elif current_room == 'Front Gate':
            print('Oh no! You\'ve been captured by the Corporate Mining Company!')
            break
        else:
            print('没有可拾取的物品!')
        invalid_move = False
    # 仅当命令无效时提示错误
    if invalid_move:
        print('\nInvalid Move')
    # 进入反派房间直接结束游戏
    if current_room == 'Front Gate':
        print('Oh no! You\'ve been captured by the Corporate Mining Company!')
        break

关键修改说明:

  1. 重复拾取问题解决:在成功拾取物品后,通过del rooms[current_room]['item']删除当前房间的item键值对,后续进入该房间时,player_stat()就不会再显示该物品,也无法再次执行拾取操作。
  2. 逻辑分支修复:添加invalid_move标记,避免成功执行移动或拾取命令后,仍触发"Invalid Move"错误提示。
  3. 异常情况处理:补充了对反派房间的判断,防止玩家在Front Gate执行Acquire时出现逻辑错误。

你之前使用del出错,大概率是因为未先判断item键是否存在就执行删除操作,现在的写法会先确认键存在再删除,不会触发报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 22:14:54