1

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?

3
  • can it be: if some is not None: instead of if not some: Commented Oct 8, 2014 at 1:13
  • Still getting the error, unfortunately Commented Oct 8, 2014 at 1:14
  • Much confusion could have been saved below if this question had been tagged python3. Please do so in future. Commented Oct 8, 2014 at 2:36

5 Answers 5

2

You're getting this error because anything given to input is expected to be a valid Python expression, but entering nothing gives

SyntaxError: unexpected EOF while parsing

Edit: In Python 3.x, input is fine - the error will only be the ValueError below.

You can remedy this problem by switching from input to raw_input, but int also can't parse an empty string, which results in another error:

ValueError: invalid literal for int() with base 10: ''

So, you can either catch the exception that is produced, or check for empty input with a conditional statement. The latter is preferable, since other errors that may occur in your code may be swallowed up with exception handling.

raw = raw_input("Enter a number: ")
if not raw:
    print "You didn't enter a number"
else:
    some = int(raw)
    print "Good job"

Note of course that you will probably still have to deal with other syntax issues, such as if a person inputs something that isn't an integer (e.g. "cat")

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to distinguish between this error and a regular ValueError? Because in my original program, I had a try, exception statement that would print an error statement when the user entered a word instead of a number. However, I want to provide a different statement if they don't enter anything. Also, because I'm using Python 3, would the raw_input still apply?
The generic error when int reaches something it can't handle is a ValueError, and trying to differentiate between specific types of errors is possible, but unnecessarily hard. Use conditional statements to match the input string with any expected cases you want to cover; i.e. if the string is empty, or if it does not contain a number, if it's a float, etc.
1

I searched and found a similar case in the official python documentation here:

while True:
    try:
        x = int(raw_input("Please enter a number: "))
        print "good job"
        break
    except ValueError:
        print "Oops!  That was no valid number.  Try again..."

update 01

code updated as below:

while True:
    try:
        x = raw_input("Please enter a number: ")
        x_mod = int(x)
        print "good job"
        break
    except ValueError:
        if len(x)==0:
            print "you entered nothing"
        else:
            print "Oops!  That was no valid number.  Try again..."

Comments

0

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")

Comments

0

You have to use raw_input and then a try-catch to type check your input.

data = raw_input('Enter number: ')
try:
    print('You gave me the number: %i' % int(data))
except:
    print("I need a number")

Comments

0

@user3495234 You may have solved this but I would solve this in following way.

import re

some = input("Enter a number?\n>")
reg = re.compile('^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$')  
# A regexp from the perldoc perlretut

if some == "":
    print("Please enter something.")
elif reg.match(some):
    x = float(some)  # To avoid type casting error
    y = int(x)  # Apparently int(float) = int
    print("Good job")
    print(y)  # Checking for value is integer or not.
else:
    print("Looks like you didn't enter a number.")

I tried using isdigit() method to distinguish between the user entering words instead of numbers but that doesn't work well with float. See this.

Updated regular expression to perldoc perlretut one because previous one was giving error in some cases.

1 Comment

If there is anything that I can do to improve my answer let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.