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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:09:40  来源:igfitidea点击:

Greeting program

pythoneclipseif-statementnew-operatorpydev

提问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-pydevit won't allow me to use the ifstatement. 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 ifstatement 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_inputinstead of input.

使用raw_input代替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 calebinto the prompt with inputit's trying to evaluate calebwhich looks like a variable. The variable calebhasn't been defined, so it's raising that exception.

当您输入caleb提示时,input它会尝试评估caleb哪个看起来像一个变量。该变量caleb尚未定义,因此它引发了该异常。

回答by jdi

The reason is that you are using the inputfunction which expects that the user will input a string that can evaluate to a python expression. Try changing it to raw_inputwhich will not try to eval, but rather give you a raw string. Also, try just doing your print statement like: print "Hello", nameYou 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.