I am getting an error when I try to run this simple script:
input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)
Let's say I type in "dude", the error I am getting is:
line 1, in <module>
input_variable = input ("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined
I am running these scripts with Python 2.7.
TL;DR
input
function in Python 2.7, evaluates whatever your enter, as a Python expression. If you simply want to read strings, then use raw_input
function in Python 2.7, which will not evaluate the read strings.
If you are using Python 3.x, raw_input
has been renamed to input
. Quoting the Python 3.0 release notes,
raw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it with the trailing newline stripped. It raisesEOFError
if the input is terminated prematurely. To get the old behavior ofinput()
, useeval(input())
In Python 2.7, there are two functions which can be used to accept user inputs. One is input
and the other one is raw_input
. You can think of the relation between them as follows
input = eval(raw_input)
Consider the following piece of code to understand this better
>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'
input
accepts a string from the user and evaluates the string in the current Python context. When I type dude
as input, it finds that dude
is bound to the value thefourtheye
and so the result of evaluation becomes thefourtheye
and that gets assigned to input_variable
.
If I enter something else which is not there in the current python context, it will fail will the NameError
.
>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined
Security considerations with Python 2.7's input
:
Since whatever user types is evaluated, it imposes security issues as well. For example, if you have already loaded os
module in your program with import os
, and then the user types in
os.remove("/etc/hosts")
this will be evaluated as a function call expression by python and it will be executed. If you are executing Python with elevated privileges, /etc/hosts
file will be deleted. See, how dangerous it could be?
To demonstrate this, let's try to execute input
function again.
>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude
Now, when input("Enter your name: ")
is executed, it waits for the user input and the user input is a valid Python function invocation and so that is also invoked. That is why we are seeing Enter your name again:
prompt again.
So, you are better off with raw_input
function, like this
input_variable = raw_input("Enter your name: ")
If you need to convert the result to some other type, then you can use appropriate functions to convert the string returned by raw_input
. For example, to read inputs as integers, use the int
function, like shown in this answer.
In python 3.x, there is only one function to get user inputs and that is called input
, which is equivalent to Python 2.7's raw_input
.