我正在制作一个收集物品的地下城游戏。我有一个if
语句,它查看我的分类物品清单,如果我收集了所有物品就触发。下一个elif
语句在我进入出口房间但没有收集所有物品时触发。下一个elif
语句应该在我收集了所有物品并进入出口房间但无法让它工作时触发。
def main():
# an empty inventory to be added to as player progresses.
inventory = []
# a dictionary of rooms and items.
rooms = {
'your apartment': {'west': 'occult bookstore', 'south': 'McDally\'s Pub'},
'occult bookstore': {'east': 'your apartment', 'item': 'skull'},
'McDally\'s Pub': {'north': 'your apartment', 'east': 'Shedd Aquarium', 'south': 'Wrigley Field',
'item': 'energy potion'},
'Wrigley Field': {'north': 'McDally\'s Pub', 'west': 'your office', 'item': 'wizard staff'},
'your office': {'east': 'Wrigley Field', 'item': 'Faerie Queen'},
'Shedd Aquarium': {'west': 'McDally\'s Pub', 'north': 'Field Museum of Natural History',
'item': 'enchanted duster'},
'Field Museum of Natural History': {'south': 'Shedd Aquarium', 'north': 'Saint Mary of the Angels church',
'item': 'blasting rod'},
'Saint Mary of the Angels church': {'south': 'Field Museum of Natural History', 'item': 'shield bracelet'}
}
# set start room.
current_room = 'your apartment'
# start_game()
while True:
# sort inventory.
sorted_inv = sorted(inventory)
# display instructions.
instructions()
# establish win condition.
if sorted_inv == ['blasting rod', 'enchanted duster', 'energy potion', 'shield bracelet', 'skull',
'wizard staff']:
print('\nYou\'ve got your stuff back. Time to see the Queen.')
elif current_room == 'your office' and sorted_inv != ['blasting rod', 'enchanted duster', 'energy potion',
'shield bracelet', 'skull', 'wizard staff']:
print('You were unprepared. The Queen tears you head off. Game over.')
break
elif current_room == 'your office' and sorted_inv == ['blasting rod', 'enchanted duster', 'energy potion',
'shield bracelet', 'skull', 'wizard staff']:
print('The Faerie Queen has terrorized your city for the last time.')
break
我试图给予你看尽可能少的代码,所以让我知道,如果我需要更多。问题代码是在while True
语句。第一个if
和elif
语句工作,但最后elif
不。我试图使它成为一个else
语句,但也没有触发。任何帮助将不胜感激。
1条答案
按热度按时间xlpyo6sf1#
如果先前的
if
(或elif
)为true。您的第二个elif
测试的内容与原始if
测试的内容相同,外加第二个条件,但由于如果elif
* 可以 * 触发,原始if
将始终触发,elif
测试将永远不会执行。重新安排测试以使其正常工作,并(主要)避免冗余检查:注意,
sorted_inv
只需要比较一次;如果它在第一个条件中匹配,那么如果剩下的唯一elif
触发,它肯定不匹配。