问候方案 [英] Greeting program

查看:200
本文介绍了问候方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习如何使用PythonPython绝对初学者指南来编程Python。我遇到的问题是,当使用 eclipse-pydev 时,不允许我使用如果语句。这是我写的代码...

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 )

结果是

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

当我执行 if 语句我把

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

结果是

  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    


推荐答案

p>使用 raw_input 而不是 输入

Use raw_input instead of input.

Python开发人员可能应该重命名这些功能,以便更清晰,初学者不要轻易混淆。

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

当您输入 caleb 进入输入的提示,它试图评估 caleb ,它看起来像一个变量。变量 caleb 尚未定义,因此正在引发该异常。

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.

这篇关于问候方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆