'I'm trying to create a text-based game where you have to avoid a monster while collecting Items [duplicate]

The code for the rooms works but changing the rooms doesn't the code also loops to the start even when the required part to move on is obtained. The game over will be put in along with the code for the monster's movements so that when the monster and the player are in the same room the game ends in a game over. The monster's movements are going to be random so game-overs will be random.

    'Entrance Hall': {'name': 'Entrance Hall', 'exits': {'North': 'Great Hall', 'West': 'Bathroom'}},
    'Great Hall': {'name': 'Great Hall', 'exits': {'West': 'Bedroom', 'East': 'Kitchen', 'North': 'Throne Room',
                                                   'South': 'Entance Hall'}},
    'Bedroom': {'name': 'Bedroom', 'exits': {'North': 'Great Hall', 'East': 'Cellar', 'West': 'Bathroom'}},
    'Cellar': {'name': 'Cellar', 'exits': {'West': 'Bedroom'}},
    'Attic': {'name': 'Attice', 'exits': {'South': 'Throne Room'}},
    'Kitchen': {'name': 'Kitchen', 'exits': {'North': 'Celler', 'West': 'Great Hall'}},
    'Bathroom': {'name': 'Bathroom', 'exits': {'East': 'Bedroom', 'North': 'Entarnce Hall'}},
    'Throne Room': {'name': 'Throne Room', 'exits': {'South': 'Great Hall', 'North': 'Attic'}},
}

def game():
    """starts the game"""
    game_over = False
    quit = False
    answer = input('Start game? y/n?')
    if answer.lower() == 'y':
        print('Welcome to a world of unknown creatures and mazes')
        print('You wake up in the Entrance hall of an Abandoned Castle')
        current_room = rooms['Entrance Hall']
        inventory = []
        while current_room == rooms['Entrance Hall']:
            print(current_room)
            print('You see two doors one has an ornate gold trim around it the other is plain')
            print('the door with the ornate trim leads to the Great Hall')
            print('The other door leads to the bathroom')
            answer = input('Which room do you want to check out first?')
            if answer.lower() == 'Bathroom':#to change the current room to the bathroom
                current_room = rooms['Bathroom']
                print(current_room)
                break
            elif answer.lower() == 'Great Hall':#same as above but for the great hall
                current_room = rooms['Great Hall']
                break
            elif answer.lower() == 'q' or 'Quit':#first idea for the quit
                pass
        while current_room == rooms['Bathroom']:
            print(current_room)
            if 'Key Fragment 1' is not in inventory:
                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)
                    else:
                        print('You left the key fragment)
                else:
                    print('You decided to leave the mysterious object alone')
            while 'Key fragment 1' in inventory:
                print('There are two doors')
                print('The first is the door to the Entrance Hall')
                print('The other goes to a Bedroom')
                pass
    else:
        print('Maybe later')


game()

while I can get some of the code to work I cannot get the game to change from the Entrance Hall to the other rooms then I need to make a quit command. I'm just learning Python and the book hasn't helped me much. While testing the code it kept looping one part and would force the yes answer even when the no answer is typed. The initial input works fine but the rest doesn't. no problems show on the debug screen and it doesn't crash on me. What would be the best way to fix the code so it works but doesn't loop or force answers? Also, what would be the best way to add the other rooms to the code so that when the item in the room is collected it allows for movement to other rooms. Finally, I need help with the code to end the game once all the items are collected.



Solution 1:[1]

The method str.lower(), which you are calling in the branch checks returns strings converted to lowercase, so all of the checks you are using to change rooms will always be false. Try replacing

if answer.lower() == 'Bathroom'

with

if answer.lower() == 'bathroom'

and so on.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zap