在房间间移动调试



下面是我必须为class创建的文本游戏的第一部分代码。一切正常,直到你把它扔进调试器并开始测试命令。

我真的不知道如何有效地使用调试器。所以我觉得在这里提问是个不错的选择。这是一个让我出错的例子。当我说GO WEst或make it all CAPS除此之外,它还会给我错误代码。

# The dictionary links a room to other rooms.
# saving for items = {} inventory for next step.
Rooms = {
'Upper Room1': {'west': 'Upstairs'},
'Upstairs': {'south': 'The Counter', 'east': 'Upper Room1'},
'Side Room': {'east': 'The Counter'},
'The Counter': {'south': 'Backroom', 'west': 'Side Room', 'east': 'Storage Room', 'north': 'Upstairs'},
'Storage Room': {'west': 'The Counter', 'north': 'Hidden Storage',},
'Hidden Storage': {'south': 'Storage Room'},
'Backroom': {'north': 'The Counter', 'east': 'The Villain'},
'The Villain': {'west': 'Bedroom'}
}
# Start the player in the first room
currentRoom = 'The Counter'
print('Welcome to the Drink Quest!') # Welcomes player to the game
print("Move commands: go North, go East, go West, go South, exit") # Informs the player of the commands of the game
# loop forever
while True:
if currentRoom.lower() == 'exit':
break
# print current status
print(' ---------------------------')
print('You are in ' + currentRoom)
# get action from user
print("Where do you want to go? ")
navigate = input('>')
navigate = navigate.split()
# Navigation
if navigate[0].lower() == 'go':
if navigate[1].lower() in Rooms[currentRoom]:
currentRoom = Rooms[currentRoom][navigate[1]]
else:
print('You can't go that way!')
elif navigate[0].lower() == 'exit':
currentRoom = 'exit'
else:
print("Invalid Choice choose a different path!")
print("Thanks for playing Drink Quest!!")

currentRoom = Rooms[currentRoom][navigate[1]]

改为

currentRoom = Rooms[currentRoom][navigate[1].lower()]

另外:创建两个变量commanddirection,以避免重复调用.lower()。然后按如下方式使用它们:

# Navigation
command = navigate[0].lower()
direction = navigate[1].lower()
if command  == 'go':
if direction  in Rooms[currentRoom]:
currentRoom = Rooms[currentRoom][direction]

最新更新