通过字典在房间之间移动



我已经找到了实现这一功能的代码。我唯一的问题是如何使用字典,而不是只做一堆";如果";带有注释的语句需要匹配我的注释,而不是字典。

我希望得到与我在代码中实现的结果相同的结果,但使用键或/和值的实际比较来选择选项,反之亦然。

rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
location = 'Great Hall'  # Starting location for the player
# print('n', *rooms[location].keys())
direction = ''  # assigning a value for user input ensuring it starts blank

def instructions():
print("Welcome to the Milestone for Module 6.")
print("Current instructions are as follows.")
print("Type the right direction to go to the next room")
print("When you move onto the game it will assist you with the right direction")
print("You can type exit to leave the game after this menu")
print("press Enter to start the game")

instructions()
input()

def main():
if location == "Great Hall":
print("Current Choice is South.")
elif location == "Bedroom":
print("Current Choice is North or East")
elif location == "Cellar":
print("Current Choice is West")

while direction != 'Exit':
print("nyou are currently held in", location)  # Stating the current position player is in
choices = rooms[location].keys()
print("Which direction would you like to choose?")
main()
direction = input()
print("You entered", direction)
if location == 'Great Hall':
if direction == "South":
location = 'Bedroom'
elif direction == "Exit":
print()
else:
print("Wrong Input")
elif location == "Bedroom":
if direction == "North":
location = "Great Hall"
elif direction == "East":
location = "Cellar"
elif direction == "Exit":
print()
else:
print("Wrong input")
elif location == "Cellar":
if direction == "West":
location = "Bedroom"
elif direction == "Exit":
print()
else:
print("Wrong input")

Her是一种用函数替换连续测试的方法:

rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def possible_directions(room):
print(f'Possible choices are: {" or ".join(rooms[room])}')
possible_directions('Bedroom')

输出:

Possible choices are: North or East

最新更新