Search code examples
pythoninputenter

How do I get python to recognize that there has been no input


I would like to create a program in which if the user presses the enter key without typing in anything, python would recognize the lack of input and relocate it to the appropriate actions. For example:

some = int(input("Enter a number"))

if not some:
    print("You didn't enter a number")

else:
    print("Good job")

What I would like to happen is if the user pressed the enter key without typing a value, they would receive the first statement. However, currently I only receive an error. Thank you.

Edit: I've had various responses about putting a try catch statement. Actually, in my original code I had a error handling statement for a ValueError. However I would like to distinguish between the user entering words instead of numbers and the user not entering anything. Is this possible?


Solution

  • I think this is what you're looking for

    try:
        some = int(input("Enter a number"))
        print("Good job")
    except (SyntaxError, ValueError):
        print("You didn't enter a number")