`elif` statement is not triggering in my `if` statement. Not sure why. Involves comparing a win condition list to an inventory list

huangapple go评论70阅读模式
英文:

`elif` statement is not triggering in my `if` statement. Not sure why. Involves comparing a win condition list to an inventory list

问题

我正在制作一个物品收集迷宫游戏。我有一个if语句,它查看我的已排序的库存列表,并在我收集了所有物品时触发。下一个elif语句会在我进入出口房间但没有所有物品时触发。下一个elif语句应该在我拥有所有物品并进入出口房间时触发,但我无法让它工作。

def main():
    # 一个空的库存,将随着玩家的进展而添加。
    inventory = []

    # 房间和物品的字典。
    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'}
    }

    # 设置起始房间。
    current_room = 'your apartment'

    while True:
        # 对库存进行排序。
        sorted_inv = sorted(inventory)

        # 显示指令。
        instructions()

        # 设定胜利条件。
        if sorted_inv == ['blasting rod', 'enchanted duster', 'energy potion', 'shield bracelet', 'skull', 'wizard staff']:
            print('\n你已经找到了你的物品。是时候见女王了。')
        elif current_room == 'your office' and sorted_inv != ['blasting rod', 'enchanted duster', 'energy potion', 'shield bracelet', 'skull', 'wizard staff']:
            print('你没有准备好。女王扯下你的头。游戏结束。')
            break
        elif current_room == 'your office' and sorted_inv == ['blasting rod', 'enchanted duster', 'energy potion', 'shield bracelet', 'skull', 'wizard staff']:
            print('仙子女王已经够恐吓你的城市了。')
            break

问题的代码在while True语句下面。第一个ifelif语句有效,但最后一个elif语句不起作用。我已经尝试将它变成else语句,但也没有触发。任何帮助将不胜感激。

英文:

I'm crafting an item collecting dungeon game. I have an if statement that looks at my sorted inventory list and triggers if I have collected all of the items. The next elif statement triggers if I enter the exit room and don't have all the items. The next elif statement is supposed to trigger when I have all items and enter the exit room but I cannot get it to work.

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

I'm trying to give you as little code to look through as possible, so let me know if I need to give more.
The problem code is under the while True statement. The first if and elif statements work but the last elif does not. I've tried making it an else statement but that also does not trigger. Any help would be greatly appreciated.

答案1

得分: 2

以下是翻译好的部分:

elif` 条件无法触发如果它被链接到的前一个 `if`(`elif`)是真的您的第二个 `elif` 正在测试与原始 `if` 相同的内容再加上第二个条件但由于原始 `if` 总是在 `elif` *可能* 触发时触发,`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("You'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` 触发它肯定不会匹配
英文:

elif conditions can't fire if a prior if (or elif) it was chained to was true. Your second elif is testing the same thing as the original if, plus a second condition, but since the original if will always fire if the elif could fire, the elif test will never be performed. Rearrange the tests to make it work sanely, and (mostly) avoid redundant checks:

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

Note that sorted_inv only has to be compared once; if it matches in the first condition, it definitely didn't match if the sole remaining elif fires.

huangapple
  • 本文由 发表于 2023年2月16日 12:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定