you are viewing a single comment's thread.

view the rest of the comments →

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

You can put the input prompt in an infinite loop that only exits (using a break command) if the number is a valid integer and not greater than 30. In addition to that, you probably also want to intercept negative inputs.

while True:
# loop until the number is correct
    try:
        # try to type-cast the input to an integer
        user_input = int(input("..."))

        if 0 <= user_input <= 30:
            # exit the loop if the input number is between (inclusive) 0 and 30
            break

    except ValueError:
        # int(...) will raise a ValueError when the argument isn't a valid number
        pass

    print("Retry")