def show_instructions():
if __name__ == "__main__":
while True:
print('Dragon Text Game\n')
print('\t\t\tDragon Text Game.')
print('---------------------------------------------------------------------------------------
------')
print('Move commands: South, North, East, West')
print('Type "Exit" to end the game.')
print("Let's start with your name: ")
name = input()
print(f'Good luck, {name}!')
# A dictionary for the simplified dragon text game
# The dictionary links a room to other rooms.
rooms = {
'Great Hall': {'name': 'Great Hall', 'South': 'Bedroom',
'text': 'You are in the Great Hall.'},
'Bedroom': {'name': 'Bedroom', 'North': 'Great Hall', 'East': 'Cellar',
'text': 'You are in the Bedroom.'},
'Cellar': {'name': 'Cellar', 'West': 'Bedroom',
'text': 'You are in the Cellar. Type "Exit" to end the game!'}
}
directions = ['North', 'South', 'East', 'West']
current_room = rooms['Great Hall']
exit = False
# game loop
while True:
print(current_room['text'])
command = input('\nEnter your move: ')
if command == 'Exit':
if current_room['name'] == 'Cellar':
# The game completes when the user enters Cellar Room
print('Congratulations !!')
print("You've completed 'Dragon Text Game'.")
print('Thanks for playing!')
break
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
else:
print(f'No door in {command} direction!')
else:
print('Invalid move!')输出:
Dragon Text Game.移动命令:南,北,东,西类型“出口”结束游戏。让我们从你的名字开始:
祝你好运,瑞布!你在大会堂里。
输入您的移动:南方无效移动!你在大会堂里。
输入您的移动:东方无效移动!你在大会堂里。
输入您的移动:退出无效移动!你在大会堂里。
输入您的移动:
一直在重复。在PyCharm中创建,因此所有缩进、空格等都是正确的,尽管您在上面看到了这些。这应该非常简单。我只需要在任何时候都能离开,从字典打电话到另一个房间。我不知道我做错了什么。谢谢!
发布于 2022-10-22 14:16:57
尝试将directions更改为
directions = ['NORTH', 'SOUTH', 'EAST', 'WEST']并将if command in directions:改为if command.upper() in directions:
您的代码当前无法工作,因为它区分大小写。如果您进入,South而不是south,您很可能可以从一个房间移动到另一个房间。通过强制所有输入都大写,可以避免此问题。为检查if command.upper() == 'EXIT'执行类似的操作
https://stackoverflow.com/questions/74164274
复制相似问题