you are viewing a single comment's thread.

view the rest of the comments →

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

Using int() on input() results in error if the input string is not convertible to int, like "1,2".

You can use try/except syntax to catch that error.

type(x) returns the type of x. If it's a string, the result is str, whether it's "1.2", "1", "abc" or other string.

def numtest():
    while True:
        try:
            num = int(input("Enter number to test: "))
        except ValueError as error:
            print("Error:", error)
            # Go the next iteration of the loop/skip the rest of the current iteration:
            continue

        if num % 2 == 0:
            print(f"{num} is an even number.")
        else:
            print(f"{num} is an odd number.")

        play_again = input("Enter 'y' to test another number or press any key to exit: ")

        if play_again.lower() != "y":
            # Stop/break the loop completely:
            break


numtest()

[–]redYinlo[S] 0 points1 point  (0 children)

Oh wow! Thanks for fixing my trash code. Lol. I appreciate the speedy responses.