1

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

Sergei
  • 585
  • 4
  • 9
  • 21

3 Answers3

4

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().

Corey Farwell
  • 1,856
  • 3
  • 14
  • 19
2

Try raw_input instead of input.

To expand, you're probably reading a tutorial that assumes you're using Python 3.x, and you're probably using Python 2.x. In Python 2.x, input actually evaluates the text it gets, which means that it thinks haloj is a variable name; hence the NameError. In Python 3.x, input simply returns the text as a string.

Your other option is to enter 'haloj' instead of haloj. (Note the added quotation marks.)

senderle
  • 145,869
  • 36
  • 209
  • 233
0

For this purpose, you should use raw_input() instead of input(). When you use input(), python actually is doing an eval(raw_input()) - so it is trying to call eval on what you input. When you typed in 'haloj', python looked for a variable called haloj, which, of course, did not exist.

caleb
  • 2,687
  • 30
  • 25