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

Python

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 "Niraj", the error I am getting is:

line 1, in <module>
input_variable = input ("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'Niraj' is not defined

1
Answers

Replies


In Python 2.7, when you read something through input function, it will be considered as a Python expression. If you want to take in string inputs, you have to use the raw_input function like below.



input_variable = raw_input ("Enter your name: ")


 

 
 

If you want to unleash your potential in this competitive field, please visit the Python course page for more information, where you can find the Python tutorials and Python frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.