【发布时间】:2021-04-09 22:09:59
【问题描述】:
您好,我是使用 python 编码和创建基于文本的游戏的新手。我正在编写代码以从一个房间移动到另一个房间。我需要一些帮助来清理我到目前为止所做的事情。我通过一个即时反馈程序运行它并获得了这些提示,但不确定如何纠正它:
- Great Hall 字符串是主词典中的一个键。修改代码,使其不是主字典中的键。
- 它说它无法对我的代码进行分类(不确定这是什么意思)
- 将多个打印命令整合到一个函数中
- 使用非复杂条件使条件更简单
- 更好的做法是使用 while True: 和 break 保留字在需要停止循环时停止循环
数据设置
rooms = {'Great Hall': {'name': 'Great Hall', 'South': 'Bedroom', 'North': 'Dungeon', 'West':
'Library', 'East': 'Kitchen'},
'Bedroom': {'name': 'Bedroom', 'East': 'Cellar', 'North': 'Great Hall'},
'Cellar': {'name': 'Cellar', 'West': 'Bedroom'},
'Library': {'name': 'Library', 'East': 'Great Hall'},
'Kitchen': {'name': 'Kitchen', 'West': 'Great Hall', 'North': 'Dining Room'},
'Dining Room': {'name': 'Dining Room', 'South': 'Kitchen'},
'Dungeon': {'name': 'Dungeon', 'South': 'Great Hall', 'East': 'Gallery'},
'Gallery': {'name': 'Gallery', 'West': 'Dungeon'},
}
directions = ['North', 'South', 'East', 'West']
current_room = rooms['Great Hall']
游戏循环
while True:
# display current location
print()
print('You are in the {}.'.format(current_room['name']))
# get user input
command = input('\nWhat direction do you want to go? ').strip()
# movement
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
else:
# bad movement
print("You can't go that way.")
# Exit game
elif command.lower() in ('q', 'quit'):
break
# bad command
else:
print("I don't understand that command.")
【问题讨论】:
-
第一个建议是使用
f strings,它们更容易阅读 -
我也不认为这是一个合适的问题
-
@ClMend 他的代码没有问题,也不是一般问题,因此根据 SO(据我所知)它不被认为是一个好问题,它更适合代码审查
标签: python