已关闭,此问题需要更focused。它目前不接受回答。
**想改善这个问题吗?**更新问题,使其只关注editing this post的一个问题。
8天前关闭
Improve this question
我们被要求做一个基于文本的游戏,你必须在面对恶棍之前穿过房间收集所有物品。如果你没有所有的项目之前,进入房间的恶棍你输了。
我的代码有问题。我可以移动房间到房间,看看每个房间里有什么物品,我也可以看到它所在的房间里的恶棍。我拿不起房间里的东西,也不知道我哪里做错了,我也不知道为什么当我和恶棍一起进入房间时,我没有输。
我是新的编码,所以任何帮助将不胜感激。这也是我在这里的第一篇文章,我可以看到我的字典看起来格式奇怪,但它确实适合Pycharm正确。
rooms = {
'Rec Room': {'North': 'Sleeping Quarters', 'South': 'Decontamination', 'East': 'Mess Hall', 'West': 'Med Bay',
'item': 'Nothing'},
'Decontamination': {'North': 'Rec Room', 'South': 'Terrarium', 'East': 'Water Treatment', 'West': 'Lab',
'item': 'Empty Laser Weapon'},
'Terrarium': {'North': 'Decontamination', 'item': 'The Alien!'},
'Lab': {'North': 'Med Bay', 'East': 'Decontamination', 'item': 'Recording'},
'Med Bay': {'North': 'Cargo Hold', 'South': 'Lab', 'East': 'Rec Room', 'item': 'Medical Supplies'},
'Cargo Hold': {'South': 'Med Bay', 'East': 'Sleeping Quarters', 'item': 'MKIV Suit'},
'Sleeping Quarters': {'South': 'Rec Room', 'East': 'Bathroom', 'West': 'Cargo Hold', 'item': 'Artifact'},
'Bathroom': {'South': 'Mess Hall', 'West': 'Sleeping Quarters', 'item': 'MKV Helmet'},
'Mess Hall': {'North': 'Bathroom', 'South': 'Water Treatment', 'West': 'Rec Room', 'item': 'Ammo'},
'Water Treatment': {'North': 'Mess Hall', 'West': 'Decontamination', 'item': 'Shield Charge'}
}
# Add Directions Here
def show_instructions():
print('---------------')
print('Welcome to Lost Lab') # Add Name of Game
print('Collect all 8 Items to Win the Game, or Be Prepared to Face the Alien!')
print('Move Commands: North, South, East, West')
print('Add to Inventory: Get "Item Name" ')
print('---------------')
# Start Player In Rec Room
starting_room = 'Rec Room'
current_room = starting_room
show_instructions()
inventory = []
# Start Loop Giving Status
while True:
print('You are in the {}'.format(current_room))
print('Inventory:', inventory)
if 'item' in rooms[current_room]:
print('You See ' + rooms[current_room]['item'])
if len(inventory) != 8 and 'item' == 'The Alien!':
print('You were not prepared, The Alien has defeated you!')
break
if len(inventory) == 8 and 'item' == 'The Alien!':
print('You Did It! You have conquered The Alien!')
break
# Movement
move = input('Enter Your Move: ').split()[-1].title()
print('---------------')
# Add .capitalize to make sure command works no matter what case is used
# To Move To Next Room and Begin Item Pickup
if move in rooms[current_room]:
current_room = rooms[current_room][move]
elif move.title() == ('Get' + rooms[current_room]['item']):
if rooms[current_room]['item'] in inventory:
print('You already have this item')
else:
inventory.append(rooms[current_room]['item'])
# Invalid Direction
else:
print('Invalid Move, You can not go that way!'.format(move))
1条答案
按热度按时间lf5gs5x21#
当你以这种方式接受输入时,你只会得到输入的最后一个单词。这就是
.split()[-1]
所做的。因此,如果您输入“Get”,“Get”将被剥离,移动最终仅为““。
所以稍后当你将输入与
'Get' + rooms[current_room]['item']
进行比较时,当然它是不相等的,因为move不包含“Get”。在这种情况下,如果你不明白为什么会发生某些事情,打印变量通常是非常有帮助的,这样你就可以看到为什么它是无效的。
实际上,看起来你试图这样做,因为打印的消息在末尾有
.format(move)
,这意味着你想在消息中插入move
变量。但是该消息不包含
{}
占位符,因此它不知道在哪里插入move
变量,因此没有插入。