def nextroom(currentloc, direction):
rooms = {
'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
'Parents Office': {'North': 'Invalid','South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid', 'Item': 'Golf Club'},
'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Living-room', 'Item': 'Bucket of Marbles'},
'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Bedroom', 'Item': 'Handcuffs' },
'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}
return rooms[currentloc][direction]
def player_stat(location):
rooms = {
'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
'Parents Office': {'North': 'Invalid', 'South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid','Item': 'Golf Club'},
'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Living-room', 'Item': 'Bucket of Marbles'},
'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Bedroom', 'Item': 'Handcuffs'},
'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}
print('----------------------------')
print('You are in {} with {}'.format(location, rooms[location]['Item']))
print('----------------------------')
def play():
currentRoom = 'Bedroom'
while currentRoom != 'Exit' or 'exit':
player_move = input('Enter your move:n')
if player_move == 'Exit' or player_move == 'exit':
currentRoom = 'Exit'
print('Play again soon!')
elif player_move == 'South' or player_move == 'south':
currentRoom = nextroom(currentRoom, 'South')
player_stat(currentRoom)
elif player_move == 'North' or player_move == 'north':
currentRoom = nextroom(currentRoom, 'North')
player_stat(currentRoom)
elif player_move == 'West' or player_move == 'west':
currentRoom = nextroom(currentRoom, 'West')
player_stat(currentRoom)
elif player_move == 'East' or player_move == 'east':
currentRoom = nextroom(currentRoom, 'East')
player_stat(currentRoom)
以上是我所创造的基于文本的游戏的部分代码。我在定义"无效"时遇到了麻烦。在while循环中,rooms字典中的值。当用户访问无效房间时,我希望它打印一个str。
谢谢
下面是我从我这边得到的错误,当我试图进入一个房间('North': 'Invalid')
Traceback (most recent call last):
File "/Users/jacenduplain/PycharmProjects/pythonProject3/1.py", line 111, in <module>
play()
File "/Users/jacenduplain/PycharmProjects/pythonProject3/1.py", line 91, in play
player_stat(currentRoom)
File "/Users/jacenduplain/PycharmProjects/pythonProject3/1.py", line 75, in player_stat
print('You are in {} with {}'.format(location, rooms[location]. ['Item']))
KeyError: 'Invalid'
修改nextroom()
检查是否为无效房间。如果是,它可以打印一条消息并返回原来的房间。
def nextroom(currentloc, direction):
rooms = {
'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
'Parents Office': {'North': 'Invalid','South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid', 'Item': 'Golf Club'},
'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Living-room', 'Item': 'Bucket of Marbles'},
'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Bedroom', 'Item': 'Handcuffs' },
'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}
next = rooms[currentloc][direction]
if next.lower() == 'invalid':
print("You can't go that direction!")
return currentloc
else:
return next