出现错误但不明白原因或如何修复它

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

我在学校,正在努力解决这个项目。它只发生在一个区域。我可以去每个房间,得到每件物品。但是,如果我去车库并尝试回来,我会收到阳光房的“KeyError” 它指出:

Traceback (most recent call last):
  File "C:\Users\Josh\PycharmProjects\TextBasedGame\TextBasedGame.py", line 46, in <module>
    if current_room != 'Basement' and 'item' in rooms[current_room].keys():
                                                ~~~~~^^^^^^^^^^^^^^
KeyError: 'Sun Room'

我也为没有在这里写代码而道歉,因为我还在学习堆栈溢出。我不明白为什么我会得到这个,也不明白为什么只有这个房间。下面是我的代码

def main():  # greetings and instructions
    print('Welcome to the Hostage Rescue Adventure game! ')
    print('Collect all 6 items and reach the basement to defeat the criminal and save the hostage!')
    print('To move type: go north, go east, go west, go south. To get an item type: Get item')


def movement(current_room, move, rooms):  # movement
    current_room = rooms[current_room][move]
    return current_room


def get_item(current_room, move, rooms, inventory):  # adding items to inventory and deleting it from the room
    inventory.append(rooms[current_room]['item'])
    del rooms[current_room]['item']


rooms = {  # The dictionary links a room to other rooms.
    'Great Room': {'South': 'Sun room', 'North': 'Bedroom', 'West': 'Foyer', 'East': 'Kitchen'},
    'Bedroom': {'item': 'Bullet Proof Vest', 'East': 'Bathroom', 'South': 'Great Room'},
    'Sun room': {'item': 'Gun', 'North': 'Great Room', 'East': 'Garage'},
    'Kitchen': {'item': 'Bullets', 'West': 'Great Room', 'North': 'Basement'},
    'Foyer': {'item': 'Document', 'East': 'Great Room'},
    'Basement': {'South': 'Kitchen'},
    'Bathroom': {'item': 'Helmet', 'West': 'Bedroom'},
    'Garage': {'item': 'Shield', 'West': 'Sun Room'},
}

current_room = 'Great Room'  # starts player in the Great Hall
inventory = []
main()

while True:
    if current_room == 'Basement':  # if current room is the basement you win.
        if len(inventory) == 6:
            print('You got all the items, defeated the criminal and rescued the hostage! Congratulations!')
            break
        else:  # Fails the game if all items are not collected
            print('You did not collect all of the items. You did not beat the criminal and the hostage was taken!')
            print('YOU LOSE!')
            break
    print('You are in the ' + current_room)
    print(inventory)

    if current_room != 'Basement' and 'item' in rooms[current_room].keys():
        print('You see the {}'.format(rooms[current_room]['item']))
    print('------------------------------')
    move = input('Enter your move: ').title().split()

    # handle if the user enters a command to move to a new room
    if len(move) >= 2 and move[1] in rooms[current_room].keys():
        current_room = movement(current_room, move[1], rooms)
        continue
    # handle if the user enter a command to get an item
    elif len(move[0]) == 3 and move[0] == 'Get' and ' '.join(move[1:]) in rooms[current_room]['item']:
        print('You pick up the {}'.format(rooms[current_room]['item']))
        print('------------------------------')
        get_item(current_room, move, rooms, inventory)
        continue
    # handle if the user enters an invalid command
    else:
        print('Invalid move, please try again')
        continue

main()
```

I attempted to rename and move the sun room. it did not work. What did work was disconnecting the garage from the sun room and attaching it to the kitchen. I still do not understand why I got the error.

```
Great Room': {'South': 'Sun room', 'North': 'Bedroom', 'West': 'Foyer', 'East': 'Kitchen'},
    'Bedroom': {'item': 'Bullet Proof Vest', 'East': 'Bathroom', 'South': 'Great Room'},
    'Sun room': {'item': 'Gun', 'North': 'Great Room'},
    'Kitchen': {'item': 'Bullets', 'West': 'Great Room', 'North': 'Basement', 'South': 'Garage'},
    'Foyer': {'item': 'Document', 'East': 'Great Room'},
    'Basement': {'South': 'Kitchen'},
    'Bathroom': {'item': 'Helmet', 'West': 'Bedroom'},
    'Garage': {'item': 'Shield', 'North': 'Kitchen'},
python pycharm
1个回答
0
投票

如果您尝试使用错误消息中所示的键“Sun Room”键入 rooms 字典,而实际键值是带有小写 r 的“Sun room”,那么您将收到键错误,因为这些钥匙不一样!

确保当前房间的值是“Sun Room”而不是“Sun room”,并且应该正确!

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