需要帮助在调整我的代码,我无法移动到其他房间,我只能移动北和南没有其他房间正在识别.目标是能够在房间之间来回.正在使用的房间如下:阁楼,客厅,门厅,前廊,卧室,地下室,厨房,餐厅。
设计模式如下:(从阁楼开始)
阁楼(开始)-向南到客厅
客厅-向西到餐厅,向北回到阁楼,向东到门厅
门厅-向西到客厅,向北到卧室,向南到前门廊
卧室-向南到门厅
前廊-向北至门厅
餐厅-东到客厅,北到厨房,南到地下室
厨房-向南到餐厅
地下室-向北到餐厅
我的代码如下:
rooms = {
'Attic': {'South': ' Living Room'},
'Living Room': {'East': 'Foyer', 'West': 'Dining Room','North': 'Attic'},
'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
'Bedroom': {'South': 'Foyer'},
'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
'Kitchen': {'South': 'Dining Room'},
'Basement': {'North': 'Dining Room'},
'Front Porch': {'North': 'Foyer'},
}
def possible_moves():
moves = rooms[location].keys()
print("Option:", *moves)
location = 'Attic'
Direction = ''
while True:
print('You are in the', location)
possible_moves()
direction = input("Which way would you like to go? ")
if direction == 'Exit' or Direction == 'exit':
print('Thank you for playing!')
break
elif direction == 'South' or direction == 'south':
location = 'Living Room'
print('You are in the', location)
possible_moves()
direction = input("Which way would you like to go? ")
if direction == 'East' or direction == 'east':
location = 'Foyer'
print('You are in the', location)
possible_moves()
direction = input("Which way would you like to go? ")
elif direction == 'North' or direction == 'north':
location = 'Attic'
print('You are in the', location)
possible_moves()
direction = input("Which way would you like to go? ")
else:
print('Invalid')
1条答案
按热度按时间wfsdck301#
你的代码应该看起来像这样。我注意到if语句是不必要的。
紧凑而简单的解决方案。
如果你想要if语句,只需用下面的代码替换
location == rooms[location][direction.title()]
:但我建议做前面的例子。