Welcome program

I learn how to program in Python using the book "Python the Absolute Beginners Guide". The problem I am facing is that when using eclipse-pydev it will not let me use the if . Here is the code I wrote ...

 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 , 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 
+6
source share
3 answers

Use raw_input instead of input .

Python developers probably should have renamed these functions to make them easier to understand, and beginners don't get confused so easily.

When you type caleb at the input prompt, it tries to evaluate caleb , which looks like a variable. The caleb variable caleb not defined, so it raises this exception.

+9
source

The reason is because you are using the input function, which expects the user to enter a string that can be evaluated in a python expression. Try changing it to raw_input , which will not try to evaluate, but rather give you a raw string. Also, just try to make your own print statement, for example: print "Hello", name There is no comma in this first example.

+4
source
 >>> help(input) input([prompt]) -> value Equivalent to eval(raw_input(prompt)). 

Use raw_input .

+3
source

Source: https://habr.com/ru/post/899177/


All Articles