pythonstring

Name is not defined error


I am well aware that this is a noob question but i cant seem to find the solution, i'm very new to programming but eager to learn. I'm going through the first google clases in python and this is the error i get.

def both_ends(s):
  print s[0:2], s[3:5]
  return

x = input('...: ')
both_ends(x)

sergei@sergei-MS-7253:~/Desktop/p2$ python test2.py
...: haloj
Traceback (most recent call last):
  File "test2.py", line 10, in <module>
    x = input('...: ')
  File "<string>", line 1, in <module>
NameError: name 'haloj' is not defined

please explain what the problem is.

Thank you


Solution

  • In Python 2, input() takes a string that should be immediately executed. If you want to save the given input as a string (your intention), use raw_input().