python-3.x “elif”语句没有在我的“if”语句中触发,不确定原因,涉及将成功条件列表与库存列表进行比较

7xzttuei  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(98)

我正在制作一个收集物品的地下城游戏。我有一个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语句。第一个ifelif语句工作,但最后elif不。我试图使它成为一个else语句,但也没有触发。任何帮助将不胜感激。

xlpyo6sf

xlpyo6sf1#

如果先前的if(或elif)为true。您的第二个elif测试的内容与原始if测试的内容相同,外加第二个条件,但由于如果elif * 可以 * 触发,原始if将始终触发,elif测试将永远不会执行。重新安排测试以使其正常工作,并(主要)避免冗余检查:

if sorted_inv == ['blasting rod', 'enchanted duster', 'energy potion', 'shield bracelet',
                  'skull', 'wizard staff']:
    if current_room == 'your office':
        print('The Faerie Queen has terrorized your city for the last time.')
        break
    else:
        print("\nYou've got your stuff back. Time to see the Queen.")
elif current_room == 'your office':  # This test only performed if sorted_inv wasn't equal
    print('You were unprepared. The Queen tears your head off. Game over.')
    break

注意,sorted_inv只需要比较一次;如果它在第一个条件中匹配,那么如果剩下的唯一elif触发,它肯定不匹配。

相关问题