为什么我的 .append 命令工作正常时,我的 .remove 命令却不起作用

问题描述 投票:0回答:1

我正在做一个课堂项目,你可以制作一个基于文本的小游戏来介绍 Python。我的字典以及大多数用于在房间之间移动的 if/else 都工作正常。我遇到麻烦的地方是物品/库存顺序。 我有一本字典,显示 rooms 之间的连接,以及每个房间中有什么项目,然后我有 2 个列表。第一个列表包含所有物品,第二个列表是玩家库存。 这个想法是玩家输入命令来获取物品。然后代码确认该项目位于字典中的当前房间以及 item_list(这是参考列表)中。一旦发生这种情况,该物品就会添加到玩家的库存列表中。 此时,该项目应该从参考列表中删除,因此代码不再将其显示为拾取选项。然而,我正在使用的 .remove 代码行不起作用,尽管其设置与库存列表的 .append 代码完全相同,但运行完美。

代码顺序如下: 'rooms' 是字典,item_list 是参考列表,inventory 是玩家获取物品时物品所在的位置

rooms = { #defines the locations, the relationships between the locations, and locates the items not yet in inventory
        'Great Hall':
                {'South': 'South Hall', 'North': 'North Hall', 'West': 'Entrance', 'East': 'Kitchen'},
        'South Hall':
                {'North': 'Great Hall', 'East': 'Dungeon', 'item': 'a Mirror'},
        'Dungeon':
                {'West': 'South Hall', 'item': 'a Shield'},
        'North Hall':
                {'South': 'Great Hall', 'East': 'Tower', 'item': 'a Hand Mirror'},
        'Tower':
                {'West': 'North Hall', 'item': "Odin's Staff"},
        'Kitchen':
                {'West': 'Great Hall', 'North': 'Dining Room', 'item': 'a bowl of Nuts'},
        'Entrance':
                {'East': 'Great Hall', 'item': 'a Torch'},
        'Dining Room':
                {'South': 'Kitchen'}}
directions = ['North', 'South', 'East', 'West'] #defines player movement options
item_list = ['a Mirror', 'a Shield', 'a Hand Mirror', "Odin's Staff", 'a bowl of Nuts', 'a Torch'] #reference for the 'Get item' command
inventory = [] #list to be populated with items as they are collected by the player
current_room = 'Entrance' #starting point, updates to output current location when player moves
def player_status():
        print('You are in the', current_room)  # status
        print('Inventory:', inventory)
        print('You see:', rooms.get(current_room).get('item'))
while True:
        if current_room == 'Dining Room': #win condition
                if len(inventory) == 6:
                        print('Win condition text')
                else: #lose condition
                        print('Lose condition text')
                break

        player_status()
        command = input('What would you like to do?')
        if command in directions: #moves player in the direction indicated by input.
                if command in rooms.get(current_room).keys():
                        current_room = rooms[current_room].get(command)
                else: #shows if input is not in dictionary
                        print('There is nothing in that direction.')
        elif command == 'Get item': #The command for adding an item to inventory.
                if rooms.get(current_room).get('item') in item_list:
                        inventory.append(rooms.get(current_room).get('item'))
                        item_list.remove(rooms.get(current_room).get('item'))
                        print('You acquired:',rooms.get(current_room).get('item'))
                else:
                    print("There's nothing here.")
        else:
                print("I'm sorry, I don't understand that command")

inventory.append 行工作完美,但无论出于何种原因,item_list.remove 都没有执行任何操作。

帮忙? 编辑:添加了完整的代码以提高可读性

python list append
1个回答
0
投票

好的,.remove 命令工作正常,事实证明问题出在 current_room 命令中。

回到绘图板

© www.soinside.com 2019 - 2024. All rights reserved.