Home > Blockchain >  Fixing an exit function with invalid input print statements reoccuring
Fixing an exit function with invalid input print statements reoccuring

Time:12-09

My code is running pretty smooth. I have some sentence structure I want to add to the game to make it more immersive. I forgot to add an exit to my game. i created an exit and it works, now when i type in any other command, even if it works, i get a print statement of invalid input. i have moved my exit and invalid statement around the codes but i cant seem to get past this. i left the invalid statement at the bottom of my code. i want to be able to type in other commands than 'exit' and not get the invalid print statement for instance, if i type 'go North' i do move to the next room but i get an output of "invalid input" before the print statements from the room. any suggestions would be greatly appreciated.

 
# declaration
rooms = {
    'Town Square': {'North': 'Bookstore', 'South': 'Restaurant', 'East': 'Jeweler', 'West': 'Tailor', 'item': 'None'},
    'Tailor': {'East': 'Town Square', 'item': 'Suit'},
    'Restaurant': {'North': 'Town Square', 'East': 'Hair Dresser', 'item': 'Salad'},
    'Hair Dresser': {'West': 'Restaurant', 'item': 'Comb'},
    'Bookstore': {'South': 'Town Square', 'East': 'Shoemaker', 'item': 'Bible'},
    'Shoemaker': {'West': 'Bookstore', 'item': 'Shoes'},
    'Jeweler': {'North': 'Chapel', 'West': 'Town Square', 'item': 'Ring'},
    'Chapel': {'South': 'Jeweler', 'item': 'Wife'}
}
state = 'Town Square'


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


# function
def get_item(state):
    return rooms[state]['item']  # returning Item value


# function
def show_instructions():
    # print a main menu and the commands
    print("Welcome to the Wedding Adventure!")
    print("Collect all 6 items before you reach the chapel, so you will not die of embarrassment when you get left at "
          "the altar")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


show_instructions()  # calling function
inventory = []
while (1):  # gameplay loop
    print('You are in ', state)  # printing state
    print('Inventory:', inventory)  # printing inventory
    item = get_item(state)  # calling get_item function
    print('You see a', item)  # print
    print('------------------------------')
    if item == 'Wife':  # if
        if len(inventory) == 6:
            print('Congratulations! You have collected all the items and got married!')
            print('Thank you for playing the game!')
            exit(0)
        else:
            print('SLAP ... GAME OVER!')
            print('Thanks for playing! Hope you enjoyed it!')
            exit(0)
    direction = input('Enter your move: ')  # asking user
    if direction == 'go East' or direction == 'go West' or direction == 'go North' or direction == 'go South':  # if
        direction = direction[3:]
        new_state = get_new_state(state, direction)  # calling function
        if new_state == state:  # if
            print('The room has wall in that direction enter other direction!')  # print
        else:
            state = new_state  # changing state value to new_state
            print('Invalid input')
    elif direction == str('get '   item):  # Obtain item
        if item in inventory:  # if item already present in inventory
            print('Item already taken go to another room!!')
        else:
            print(f"{item} retrieved!\n")
            inventory.append(item)  # appending
    if direction == 'exit':
        print('Thank you for playing')
        exit(0)
    else:
        print('Invalid input')

My error looks like this

Enter your move: go North
Invalid input
Invalid input
You are in  Bookstore
Inventory: []
You see a Bible
------------------------------
Enter your move: exit
Thank you for playing

CodePudding user response:

You need to replace the if in the line where you check for exit with elif. The if-else structure means that the program only checks for 2 cases, meaning, every time the input is not exit it is invalid.

The elif expression in python means if all of the previous conditions were not true, then try this condition and so on..

By changing the if to elif your program won't move to the else block if any of the previous conditions (if the input is valid) was true.

while (1):  # gameplay loop
    ....
    direction = input('Enter your move: ')  # asking user
    if direction == 'go East' or direction == 'go West' or direction == 'go North' or direction == 'go South':  # if
        ....

    elif direction == str('get '   item):  # Obtain item
        ....

    elif direction == 'exit':
        print('Thank you for playing')
        exit(0)

    else:
        print('Invalid input')
  • Related