0

Good evening, I am working on a text-based game for my scripting class. This week we are incrementing the "move between rooms" process. My code is working smoothly except I cannot get the user to exit the game. When I type "Exit" It tells me "Ouch! I hit a wall." Not sure why it's going through the if statement and not the elif statement I have for exiting the game.

# Nick Thomas
# declaration
rooms = {
    'Ground Level': {'Right': 'Library', 'Up': 'Bedroom'},
    'Library': {'Left': 'Ground Level', 'Up': 'Enchanting Lab'},
    'Bedroom': {'Right': 'Enchanting Lab', 'Down': 'Ground Level'},
    'Enchanting Lab': {'Left': 'Bedroom', 'Down': 'Library'}
}
state = 'Ground Level'


# function
def get_new_state(state, direction):
    new_state = state  # declaration
    for i in rooms:  # loop
        if i == state:  # if statement
            if direction in rooms[i]:  # if statement
                new_state = rooms[i][direction]  # assigns new_state
    return new_state  # return


# FIXME: a function for getting items

# function
def instructions():
    # print a main menu and the commands
    print('Necromancer’s Lair Text Game')
    print('Move commands: go Up, go Down, go Left, go Right')
    print('To Exit: type Exit')
    # FIXME: add instructions for what do and how to get an item


instructions()
# FIXME: add empty inventory list
while 1:
    print('You are in the', state)  # prints state
    print('------------------------------')
    direction = input('Enter your move: ')  # asking user
    if direction == 'go Up' or 'go Down' or 'go Left' or 'go Right':
        direction = direction[3:]
        new_state = get_new_state(state, direction)  # calling function
        if new_state == state:  # if statement
            print('Ouch! You hit a wall.')
        else:
            state = new_state
    elif direction == 'Exit':
        print('Thanks for playing!')
        exit(0)
    else:
        print('Invalid Direction!!')
  • 1
    `direction == 'go Up' or 'go Down' or 'go Left' or 'go Right'` should be `direction in {'go Up', 'go Down', 'go Left', 'go Right'}` – BrokenBenchmark Feb 11 '22 at 03:31

1 Answers1

1

you need to change the if condition as below where direction is compared against each direction i.e. go Up, go Down, go Left and go Right

if direction == 'go Up' or direction=='go Down' or direction=='go Left' or direction=='go Right':
Hetal Thaker
  • 717
  • 5
  • 12