eclipse 问候节目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7747871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Greeting program
提问by Kbob1998
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...
我一直在使用“Python 绝对初学者指南”一书学习如何使用 Python 进行编程。我遇到的问题是使用eclipse-pydev
它时不允许我使用该if
语句。这是我写的代码...
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
当我做我的if
陈述时,我把
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
回答by mpen
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.
Python 开发人员可能应该重命名这些函数,以便更清晰,初学者不会那么容易混淆。
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.
当您输入caleb
提示时,input
它会尝试评估caleb
哪个看起来像一个变量。该变量caleb
尚未定义,因此它引发了该异常。
回答by jdi
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.
原因是您使用的input
函数期望用户输入一个可以计算为 python 表达式的字符串。尝试将其更改为raw_input
不会尝试评估,而是给您一个原始字符串。另外,尝试只执行您的打印语句,例如:print "Hello", name
您在第一个示例中缺少逗号 sep。
回答by Karoly Horvath
>>> help(input)
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
Use raw_input
.
使用raw_input
.