英文:
Building a text based game in python
问题
I am new to python and am doing a small text-based game in python. The objective is to be able to move room to room. I keep receiving a key error as python isn't recognizing one of the rooms. After I reach the living room I am then given a key error that says this:
if command in rooms[current_room].keys():
KeyError: 'Living room'
rooms = {
'Entrance room': {'East': 'Master Bedroom'},
'Master Bedroom': {'North': 'Family room'},
'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
'Dining Room': {'South': 'Living room', 'West': 'Family room'},
'Living Room': {'South': 'Kitchen'},
'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
'Dungeon': {'East': 'Secret room'}
}
instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
directions = ['go North', 'go South', 'go East', 'go West']
print(instructions)
current_room = 'Entrance room'
while True:
if current_room == 'Dungeon':
print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
break
print('You are in the {}.'.format(current_room))
command = input('\nWhat do you want to do? ')
if command in directions:
command = command.split()[1]
if command in rooms[current_room].keys():
current_room = rooms[current_room][command]
print(current_room)
else:
print('You can't go that way!')
elif command == 'exit':
print('Thanks for playing!')
break
else:
print('Invalid input')
英文:
I am new to python and am doing a small text based game in python. The objective is to be able to move room to room. I keep receiving a key error as python isn't recognizing one of the rooms. After I reach the living room I am then given a key error that says this:
if command in rooms[current_room].keys():
KeyError: 'Living room'
rooms = {
'Entrance room': {'East': 'Master Bedroom'},
'Master Bedroom': {'North': 'Family room'},
'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
'Dining Room': {'South': 'Living room', 'West': 'Family room'},
'Living Room': {'South': 'Kitchen'},
'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
'Dungeon': {'East': 'Secret room'}
}
instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
directions = ['go North', 'go South', 'go East', 'go West']
print(instructions)
current_room = 'Entrance room'
while True:
if current_room == 'Dungeon':
print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
break
print('You are in the {}.'.format(current_room))
command = input('\nWhat do you want to do? ')
if command in directions:
command = command.split()[1]
if command in rooms[current_room].keys():
current_room = rooms[current_room][command]
print(current_room)
else:
print('You cant go that way!')
elif command == 'exit':
print('Thanks for playing!')
break
else:
print('Invalid input')
答案1
得分: 0
Python字典的键是区分大小写的。你正在检查的是'Living room'而不是字典中存在的'Living Room'。请注意,字母'r'应该是大写的。
英文:
Python dictionary keys are case-sensitive. You are checking for 'Living room' instead of 'Living Room' which is present in the dictionary. Note that the letter 'r' should be a capital.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论