Reputation: 147
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?
Upvotes: 1
Views: 9458
Reputation: 147
@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.
Upvotes: 0
Reputation: 24665
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..."
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..."
Upvotes: 1
Reputation: 48
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")
Upvotes: 0
Reputation: 7657
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")
Upvotes: 2
Reputation: 5158
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")
Upvotes: 0