Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (20.3k points)

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 <module>

input_variable = input ("Enter your name: ")

File "<string>", line 1, in <module>

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?

2 Answers

0 votes
by (40.4k points)

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


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 "<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 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


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.

0 votes
by (106k points)

It looks like you are using Python 2 for this to work in Python 2, you should use raw_input:-

input_variable = raw_input ("Enter your name: ")

print ("your name is" + input_variable)

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.5k questions

29.9k answers


99.1k users

Browse Categories