pythonpython-2.7inputsyntax

Syntax error using keyboard input in a simple python code


I have this simple python code which throws errors when i run it. I am not sure what is causing this can somebody help shed some light?

Here is my code file test.py:

name = input("What's your name? ")
print("You are great" + name + "!")
age = input("Your age? ")
print("you look great at" + str(age))

and here is the error:

>>> 
What's your name? kiran

Traceback (most recent call last):
  File "C:\Users\Sunshine\Desktop\test.py", line 1, in <module>
    name = input("What's your name? ")
  File "<string>", line 1, in <module>
NameError: name 'kiran' is not defined
>>> 

Solution

  • if you are using Python 2.7 the correct way of getting a string from user input is using the method raw_input(""), input("") is for python 3.