0

the code is for picking up a key fragment then allowing the player to move on to other rooms but keeps looping while it resets the inventory.

        while current_room == rooms['Bathroom']:
            print(current_room)
            if 'Key Fragment 1' not in inventory:#loop starts here
                print('You see the shine of an object')
                answer = input('do you want to inspect it? Yes? No?')
                if answer.lower() == 'yes' or 'y':
                    print('its a key fragment')
                    answer = input('Do you take it? Yes? No?')
                    if answer.lower() == 'yes' or 'y':
                        print('You have taken the key fragment')
                        inventory.append('Key Fragment 1')
                        print(inventory) #where the loop is supposed to end but resets

                    else:
                        print('You left the key fragment')
                else:
                    print('You decided to leave the mysterious object alone')
            if 'Key fragment 1' in inventory: #occurs after the loop ends on the collection of items
                print(inventory)
                print('There are two doors')
                print('The first is the door to the entrance hall')
                print('The other goes to a bedroom')
                answer = input('Do you want to go to the bedroom or the entrance hall?')
                if answer.lower() == 'bedroom':
                    current_room = rooms['Bedroom']
                elif answer.lower() == 'entrance' or 'entrance hall':
                    current_room = rooms['Entrance Hall']

I have the room forced to the bathroom so this code starts but it always loops after getting the item and resets the whole room and inventory before picking up the item? how do I fix this? The code to get into the room works just fine but it doesn't continue after the item is taken. I am new to coding so I'm learning as I fix and debug the code.

furas
  • 134,197
  • 12
  • 106
  • 148
  • `if answer.lower() == 'yes' or 'y'` This is not the right way to check multiple values. See https://stackoverflow.com/q/14636446/494134 – John Gordon Feb 17 '22 at 16:22
  • `if answer.lower() == 'yes' or answer.lower() == 'y':` or `if answer.lower() in ('yes', 'y'):` – furas Feb 17 '22 at 18:29
  • i have fixed this but how do I stop the room's loop from resetting upon picking up the key fragment? – spartankillsp Feb 17 '22 at 22:29
  • You are appending `'Key Fragment 1'` to inventory but check for `'Key fragment 1'`. 'F' is capital on append but lower case on check. – bfris Feb 17 '22 at 22:50

0 Answers0