Input() error - NameError: name '…' is not defined

1.5K    Asked by FukudaAshikaga in Data Science , Asked on Jul 12, 2021

I am getting an error when I try to run this simple python script:

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

Lets say I type in "dude", the error I am getting is:

line 1, in 
input_variable = input ("Enter your name: ")
File "", line 1, in
NameError: name 'dude' is not defined

I am running Mac OS X 10.9.1 and I am using the Python Launcher app that came with the install of python 3.3 to run the script.

Edit: I realized I am somehow running these scripts with 2.7. I guess the real question is how do I run my scripts with version 3.3? I thought if I dragged and dropped my scripts on top of the Python Launcher app that is inside the Python 3.3 folder in my applications folder that it would launch my scripts using 3.3. I guess this method still launches scripts with 2.7. So How do I use 3.3?

Answered by yash Jaiteley

In Python 2.7, the input function is evaluated as a Python expression. But, 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, then raw_input will be renamed to input. Quoting the Python 3.0 release notes, raw_input() is 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())

In Python 2.7, there are two functions that 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'

Here, 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 has bound to the value the fourth eye and so the result of evaluation becomes the fourth eye and that gets assigned to input_variable.

If you enter something else which is not there in the current python context, then it will fail will the NameError.

>>> input("Enter your name: ")

Enter your name: dummy

Traceback (most recent call last):

  File "", line 1, in 
  File "", line 1, in

NameError: name 'dummy' is not defined

Security considerations with Python 2.7's input:

Since whatever user types are 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. 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.



Your Answer

Interviews

Parent Categories