6

I have been learning how to program in Python using the book "Python the Absolute Beginners Guide." The problem I am having is that when using eclipse-pydev it won't allow me to use the if statement. Here is the code I have written...

name = input("What is your name? ")
print(name)
print("Hello" name )

The result was

What is your name? caleb
Traceback (most recent call last):
  File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", line 6, in <module>
    name = input("What is your name? ")
  File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
    return eval(raw_input(prompt))
  File "<string>", line 1, in <module>
NameError: name 'caleb' is not defined

When I do my if statement I put

name = input("What is your name? ")
if name == ("Caleb"):
    print(" Hello Bud!")

The result was

  What is your name? Caleb
Traceback (most recent call last):
  File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", line 6, in <module>
    name = input("What is your name? ")
  File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
    return eval(raw_input(prompt))
  File "<string>", line 1, in <module>
NameError: name 'Caleb' is not defined    
CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
Kbob1998
  • 77
  • 1
  • 1
  • 6

3 Answers3

9

Use raw_input instead of input.

Python developers probably should have renamed those functions so it's more clear and beginners don't get confused so easily.

When you type caleb into the prompt with input it's trying to evaluate caleb which looks like a variable. The variable caleb hasn't been defined, so it's raising that exception.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
4

The reason is that you are using the input function which expects that the user will input a string that can evaluate to a python expression. Try changing it to raw_input which will not try to eval, but rather give you a raw string. Also, try just doing your print statement like: print "Hello", name You were missing a comma sep in that first example.

jdi
  • 90,542
  • 19
  • 167
  • 203
  • 1
    He should probably keep the format `print()`, in case he's working with 3.x, but this is right otherwise. – TorelTwiddler Oct 12 '11 at 23:55
  • @TorelTwiddler: I think he needs to add `from __future__ import print_function` to continue using it as a function in Python 2.x. And he needs a comma in there. – mpen Oct 13 '11 at 00:02
  • @Mark, Sorry, yeah, keep the comma, but `print('Hello World')` works fine in 2.x (at least 2.6+). – TorelTwiddler Oct 13 '11 at 00:03
  • Actually, now that I play with it again, `print('Hello', 'World')` does not return the same thing as `print 'Hello', 'World'`... I guess clarification on his version is required to decide that =P – TorelTwiddler Oct 13 '11 at 00:06
  • Thank you so much for helping out a newbie!!!! – Kbob1998 Oct 14 '11 at 00:31
3
>>> help(input)

input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).

Use raw_input.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176