Skip to content Skip to sidebar Skip to footer

Python - How To Break While Loop After Empty Value In A Int Turning Input?

I want to make the while loop break when empty input is typed. I know that the error is due to the int function because it cant turn to integer an empty input but how can i do what

Solution 1:

This would work:

whileTrue:
    try:
        numb = int(input("number"))
    except ValueError:
        breakif numb % 2 == 0:
        print("this number is even")
    elif numb % 2 != 0:
        print("this number is odd")

Just handle the exception if the input cannot be converted into an integer. Now, any input that is not an integer would terminate the loop.

Post a Comment for "Python - How To Break While Loop After Empty Value In A Int Turning Input?"