TL;DR
Use raw_input()
instead of input()
in Python 2.x, because input()
is "shorthand" for eval(raw_input))
.
In Python 2.7, you need to use raw_input()
instead of input()
. raw_input()
lets you read in user input as a string, while input()
is "shorthand" for eval(raw_input())
which attempts to evaluate you user input as literal Python code
This is documented in the python 2.x documentation:
[input()
is] Equivalent to eval(raw_input(prompt))
.
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError
will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input()
function for general input from users.
This however was later changed in Python 3.x. In Python 3.x raw_input()
became input()
and the old input(eval(raw_input())
) was dropped. This is documented in What's new in Python 3:
PEP 3111: raw_input()
was renamed to input()
. That is, the new input()
function reads a line from sys.stdin
and returns it with the trailing newline stripped. It raises EOFError
if the input is terminated prematurely. To get the old behavior of input()
, use eval(input())
.
(Emphasis mine)
So use raw_input()
instead of input()
in Python 2.x, because input()
is "shorthand" for eval(raw_input))
. eg. Change:
name = input ("Can you tell me your name!")
to
name = raw_input ("Can you tell me your name!")