-1

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.

zeroMana
  • 1
  • 2
  • 1
    Removing "Not sure why" and other unneeded phrases will help the readability of your question for others in the future. Try including what you've done so far, and what worked and didn't work. Try to show that you put in the research effort. If that is shown, I'll be sure to upvote your post. – Blue Robin Feb 16 '23 at 04:06

1 Answers1

2

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271