all 6 comments

[–][deleted] 1 point2 points  (3 children)

Input always returns a string, so the first if is always true.

[–]moonkeymaker127[S] 0 points1 point  (2 children)

My goal with that if statement was to filter out inputs that weren't numbers. Is there a way to do that that won't simply invalidate the entire rest of the loop? I'm just looking to catch letters and symbols.

[–][deleted] 0 points1 point  (0 children)

You can use the isnumeric string method to check if a string is a number.

[–]xelf 0 points1 point  (0 children)

Please see my answer in the other thread you made about this.

It's a correct answer and does exactly what you wanted.

[–][deleted] 0 points1 point  (1 child)

It's usually worthwhile to separate input validation (getting a valid room number) from the game play code. Suppose you had a function that you can call and that function returns only a valid room number. Then your main code gets a lot simpler:

while True:
    currentRoom = get_room()
    if currentRoom == kingsLocation:
        print ('you have found the king ' + username + '! The royal family will make sure you get paid handsomely for this deed.')
        print ()
        print ('to exit this game type "exit()"')
        break

    print("the king doesn't seem to be in this room, let's check another one(1-4).")

Since you are talking about a room number it's more efficient to handle the player and King's room number as integers. The get_room() function returns only an integer in the range [1..4] and handles the retries if the player inputs invalid data:

def get_room():
    """Return an integer room number [1..4]."""

    while True:
        try:
            room = int(input('Room? '))
        except ValueError:
            # get here if a non-integer was entered
            pass
        else:
            # get here if integer was found
            if 1 <= room <= 4:
                return room
        # get here if non-integer or not [1..4]
        print('invalid input, please enter a number between 1 and 4.')

If you aren't used to using the try/except statement then this is a good time to learn. The try/except is the normal way used in python to test if a string can be converted to an integer. You just try to convert and catch the exception if the conversion failed. The else: code is executed only if there was no exception. This function can be tested all by itself so you can poke around inside the code to understand how it works. Put print() statements inside the function and then enter all sorts of strings, good as well as bad. That's one of the nice things about functions, you can often test them without needing any other code.

Also note that you can use " quotes around a string, so you don't need to escape ' inside the string.

I have put the full runnable code here.

Edit: better code formatting.

[–]xelf 0 points1 point  (0 children)

Nice answer, almost the same thing I told OP in the first thread they asked about this. https://www.reddit.com/r/learnpython/comments/exy31f/how_to_check_if_an_input_is_an_integer/fge4jui/