0

I am fairly new to python. I am trying to get an input from the user running the script. Below is my script:

print("This is the program to test if we can get the user's input")
users_input = input("Please enter your name. Please note that it should be a single word >>> ")
print("Is this your name? ", users_input)

Going through a few websites, this seems to be enough. But when i run this script and am asked to enter the name, I type the name and as soon as I press enter, I get the below error:

Traceback (most recent call last):
 File "test_input.py", line 3, in <module>
users_input = input("Please enter your name. Please note that it should be a single word >>> ")
  File "<string>", line 1, in <module>
NameError: name 'John' is not defined

I was expecting it to print the name but rather I get this error. Not sure why.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
T Anna
  • 874
  • 5
  • 21
  • 52

4 Answers4

3

Use raw_input() instead, since you're using Python 2.7.

raw_input gets the input as text (i.e. the characters that are typed), but it makes no attempt to translate them to anything else; i.e. it always returns a string.

input gets the input value as text, but then attempts to automatically convert the value into a sensible data type; so if the user types ‘1’ then Python 2 input will return the integer 1, and if the user types ‘2.3’ then Python 2 input will return a floating point number approximately equal to 2.3

input is generally considered unsafe; it is always far better for the developer to make decisions about how the data is interpreted/converted, rather than have some magic happen which the developer has zero control over.

It is the reason why that automatic conversion has been dropped in Python 3 - essentially; - raw_input in Python 2 has been renamed to input in Python 3; and there is no equivalent to the Python 2 input magic type conversion functionality.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
1

Use raw_input() instead of input, check this page for more info

furkanayd
  • 811
  • 7
  • 19
0

print("Is this your name? ", users_input) is not how you concatenate a literal string and a variable.

print("Is this your name? " + users_input) is probably what you are trying to do.

dfundako
  • 8,022
  • 3
  • 18
  • 34
0

Python provides us with two inbuilt functions to read the input from the keyboard.

1 . raw_input ( prompt )
2 . input ( prompt )

raw_input ( ) :

This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example –

g = raw_input("Enter your name : ") 
print g 

Output :

Enter your name : John Wick
John Wick

g is a variable which will get the string value, typed by user during the execution of program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case we use typecasting.

input ( ) :

This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python. For example –

val = input("Enter your value: ") 
print(val) 

Output :

Enter your value: 345
345

When input() function executes program flow will be stopped until the user has given an input. The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional. A notable thing is that whatever you enter as input, input function convert it into a string.

raiyan22
  • 1,043
  • 10
  • 20
  • So, why was it failing for me when i was using input(). You said 'This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list.' I was entering a valid string. Why did it not identify it? – T Anna Dec 02 '19 at 17:07
  • `print("Is this your name? ", users_input)` you used comma in this line which is definitely not correct, try using a + instead of comma there and check for yourself – raiyan22 Dec 02 '19 at 17:09