Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text based game 'Exit Game' process

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!!')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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':
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading