Python输入错误: NameError: name 'test' is not defined

标签 python python-2.7

<分区>

我是 python 菜鸟,刚开始学习这个。我正在尝试按照这本书为初学者自动执行 python 实用编程的无聊内容,但在我的第一次练习中收到错误。本书使用的是python 3,我使用的是python 2.7.6

我试图通过放置括号或去掉括号来更改它,但无法弄清楚。请就此提供帮助。

错误:

Traceback (most recent call last):
  File "first.py", line 5, in <module>
    myName = input()
  File "<string>", line 1, in <module>
NameError: name 'test' is not defined

编写的代码:

print "Hello world"
print "What is your name?" #ask for their name
myName = input()
print "nice to meet you, " + myName
print "the length of your name is:"
print (len(myName))
print "What is your age?" # asking for age this time
myAge = input()
print "You will be " + str(int(myage) +3) + "in three years."

最佳答案

我猜您正在使用 Python 2.x(因为您正在使用 print 语句)。

在 Python 2.x 中,input() 函数会尝试在返回之前评估输入的值,因此当您输入 test 时,它会尝试找到变量/名称test 导致了问题。

使用 raw_input() 而不是 input()

关于Python输入错误: NameError: name 'test' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408966/

相关文章:

python - 当项目解释器设置为 conda 且 conda 未提供/列出包时,如何在 PyCharm 中安装包?

windows - 抑制消息 : "python.exe has stopped working"

字典列表的 Python 集合计数器

python - 为什么使用 ssl 认证请求返回 html?

python - 将UTC时间字符串转换为日期时间对象

python - 如何在 Python 上使用 Windows Auth 登录 MSSQL?

python - 如何将 QuantLib 添加到 virtualenv (ubuntu)

python - 使用 pygame.mixer 加载音频

python - 如何使用 zeromq 和线程或异步处理对 python 程序的多个请求?

python - 形态转换opencv noob问题