出现错误,不明白为什么或如何修复。

huangapple go评论87阅读模式
英文:

getting an error and don't understand why or how to fix it

问题

I'm in school and trying to get this project figured out. It only happens in one area. I can go to each room and I can get each item. However if I go to the Garage and try to come back out I get a 'KeyError' for the sun room
it states:

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

I also apologize for not writing this in code here as I'm still learning stack overflow too. I don't understand why I'm getting this or why its only this room. Below is my code

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'},
}
英文:

I'm in school and trying to get this project figured out. It only happens in one area. I can go to each room and I can get each item. However if I go to the Garage and try to come back out I get a 'KeyError' for the sun room
it states:

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'

I also apologize for not writing this in code here as I'm still learning stack overflow too. I don't understand why I'm getting this or why its only this room. Below is my code

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'},

答案1

得分: 1

如果你试图使用错误消息中显示的键'太阳房间'键入房间字典,而实际键值是'太阳房间',其中小写的'r',那么你将会收到一个键错误,因为这些键是不相同的!

确保当前房间的值是'太阳房间',而不是'太阳房间',这样就可以纠正错误!

英文:

If you're trying to key into the rooms dictionary with the key 'Sun Room' as shown in the error message, while the actual key value is 'Sun room' with a lowercase r, then you'll get a key error because those keys are not the same!

ensure that the value of current room is 'Sun Room' as opposed to 'Sun room' and it should correct!

huangapple
  • 本文由 发表于 2023年8月11日 00:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定