12

I have this code:

# Compare phone number  
phone_pattern = '^\d{3} ?\d{3}-\d{4}$'

phoneNumber = str(input("Please enter a phone number: "))

if re.search(phone_pattern, "258 494-3929"):  
    print "Pattern matches"
else:  
    print "Pattern doesn't match!"

When I try to type the phone number in response to the input prompt, I get an error:

Please enter a phone number: 258 494-3929  
Traceback (most recent call last):  
   File "pattern_match.py", line 16, in <module>  
     phoneNumber = str(input("Please enter a phone number: "))  
   File "<string>", line 1  
     258 494-3929  
         ^
SyntaxError: invalid syntax

Why does this happen?


This question is about a specific error that comes up specific to Python 2.x while attempting to process user input. Usually this comes from a failed attempt at implementing Asking the user for input until they give a valid response.

See also Why does the IPython REPL tell me "SyntaxError: unexpected EOF while parsing" as I input the code? for a related problem when using IPython.

1
  • Are you storing it as a string or raw? Commented Apr 7, 2010 at 0:46

4 Answers 4

20

You should use raw_input instead of input, and you don't have to call str, because this function returns a string itself:

phoneNumber = raw_input("Please enter a phone number: ")
Sign up to request clarification or add additional context in comments.

Comments

10

In Python version 2.x, input() does two things:

  1. Reads a string of data. (You want this.)
  2. Then it evaluates the string of data as if it were a Python expression. (This part is causing the error.)

The function raw_input() is better in this situation because it does #1 above but not #2.

If you change:

input("Please enter a phone number: ")

to read:

raw_input("Please enter a phone number: ")

you'll eliminate the error of the phone number not being a valid Python expression.

The input() function has tripped up so many people learning Python that starting with Python versions 3.x, the designers of the language removed the extra evaluation step. This makes input() in versions 3.x behave the same as raw_input() in versions 2.x.

See also a helpful wikibooks article.

2 Comments

calling str() is not necessary after calling raw_input() (unless this was a cut-n-paste error) :-) your other point is quite valid: use raw_input() in Python 2 and input() in Python 3... the name was changed but the function remains the same. input() in Python 2 causes problems as described by the OP, hence the reason why it's being/been removed from the language altogether (in Python 3 that is)
It's true the str() call is superfluous, as was mentioned in some of the other answers. However, it's not causing the SyntaxError.
4

The input() function actually evaluates the input that's typed into it:

>>> print str(input("input: "))
input: 258238
258238
>>> print str(input("input: "))
input: 3**3 + 4
31

It's trying to evaluate '258 494-3929' which is invalid Python.

Use sys.stdin.readline().strip() to do your read.

Comments

2

input() calls eval(raw_input(prompt)), so you want phoneNumber = raw_input("Please enter a phone number: ").strip()

See also http://docs.python.org/library/functions.html#input and http://docs.python.org/library/functions.html#raw_input

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.