【发布时间】:2021-12-13 00:22:20
【问题描述】:
我正在尝试用 Python 为我的班级的基于文本的游戏编写代码,但遇到了几个问题。首先,由于某种原因,当我尝试调试代码时,它甚至没有运行。当它工作时,每次我输入命令时,它总是会打印多行“输入有效移动”,无论命令是否有效。我在如何从每个房间收集物品并将其添加到库存中也遇到了麻烦。
rooms = {
'Living Room': {'north': "Parent's Room", 'east': "Kitchen",'south': 'Dining Room', 'west' : 'Office'},
"Parent's Room": {'east': "Amir's Room", 'south' : 'Living Room', 'item' : 'cape'},
"Amir's Room": {'west': "Parent's Room"},
'Office': {'east': 'Living Room', 'item' : 'Toy Phone'},
'Dining Room': {'north': 'Living Room', 'east': 'Garage', 'item': 'Toy Taco'},
'Garage' : {'west': 'Dining Room', 'item': 'Toy Car'},
'Kitchen' : {'west': 'Living Room', 'north' : 'Pantry', 'item' : 'Toy Fork'},
'Pantry' : {'south' : 'Kitchen', 'item' : 'Chocolate'}
}
direction = '' # Creates empty variable for direction.
location = 'Living Room' # Places user in default starting point
inventory = []
def show_status():
''' Shows player status and commands.'''
def main():
# Keep game going until player enters 'exit'
while True:
print('----------------------------')
print('You are in the {}.'.format(location))
input = input('Enter a direction to move: ').strip().lower() # Take input from user to move them in desired direction.
direction = input.split()
# Check to see if user entered valid move.
for command in rooms[location]:
if direction == command:
location = rooms[location][direction]
print(direction)
elif direction != command:
print('Enter a valid move')
【问题讨论】:
标签: python dictionary