查看: 2086|回复: 5

Input () error - NameError: name'...'the undefined

[复制链接]

9

主题

74

帖子

185

积分

注册会员

Rank: 2

积分
185
发表于 2018-9-18 16:15:27 | 显示全部楼层 |阅读模式
我尝试运行这个简单的python脚本时收到错误:
  1. input_variable = input ("Enter your name: ")
  2. print ("your name is" + input_variable)
复制代码
  1. 我输入“dude”,我得到的错误是:
  2. line 1, in <module>
  3. input_variable = input ("Enter your name: ")
  4. File "<string>", line 1, in <module>
  5. NameError: name 'dude' is not defined
复制代码

我的系统是Mac OS X 10.9.1,使用安装python 3.3附带的Python Launcher应用程序来运行脚本。
回复

使用道具 举报

10

主题

72

帖子

180

积分

注册会员

Rank: 2

积分
180
发表于 2018-9-18 16:21:37 | 显示全部楼层
本帖最后由 令狐少侠 于 2018-9-18 16:23 编辑

Python 2.7中的input函数,作为Python表达式计算输入的内容。如果你只是想读取字符串,那么在Python 2.7中使用raw_input函数,它不会评估读取字符串。
如果您使用的是Python 3.x,raw_input则已重命名为input。引用Python 3.0发行说明,
raw_input()被重命名为input()。也就是说,新input()函数从中读取一行sys.stdin并返回它,并删除尾随换行符。EOFError如果输入提前终止,它会上升。要获得旧的行为input(),请使用eval(input())
________________________________________
在Python 2.7中,有两个函数可用于接受用户输入。一个是input另一个是raw_input。您可以如下考虑它们之间的关系
  1. input = eval(raw_input)
复制代码

请考虑以下代码以更好地理解这一点
  1. >>> dude = "thefourtheye"
  2. >>> input_variable = input("Enter your name: ")
  3. Enter your name: dude
  4. >>> input_variable
  5. 'thefourtheye'
复制代码
input接受来自用户的字符串,并在当前Python上下文中计算字符串。当我输入dude作为输入时,它会发现它dude与thefourtheye值绑定,因此评估结果变为thefourtheye并将其分配给input_variable。
如果我输入当前python上下文中不存在的其他内容,则会报NameError错误。
  1. >>> input("Enter your name: ")
  2. Enter your name: dummy
  3. Traceback (most recent call last):
  4.   File "<input>", line 1, in <module>
  5.   File "<string>", line 1, in <module>
  6. NameError: name 'dummy' is not defined
复制代码

Python 2.7的安全注意事项input:
由于评估了任何用户类型,因此也会产生安全问题。例如,如果您已经在程序中加载了模块import os,然后用户输入了
os.remove("/etc/hosts")
这将被python评估为函数调用表达式,并将被执行。如果使用管理员的权限执行Python,/etc/hosts则将删除文件。这是很危险的
为了证明这一点,让我们input再次尝试执行函数。
  1. >>> dude = "thefourtheye"
  2. >>> input("Enter your name: ")
  3. Enter your name: input("Enter your name again: ")
  4. Enter your name again: dude
复制代码
现在,当input("Enter your name: ")执行时,它等待用户输入,并且用户输入是有效的Python函数调用,因此也会调用它。这就是我们Enter your name again:再次看到提示的原因。
所以,你应该使用raw_input函数,像这样
  1. input_variable = raw_input("Enter your name: ")
复制代码
如果需要将结果转换为其他类型,则可以使用适当的函数来转换返回的字符串raw_input。例如,要将输入读取为整数,请使用此int函数,如本答案中所示。
在python 3.x中,只有一个函数可以获取用户输入并且被调用input,这相当于Python 2.7 raw_input。
回复

使用道具 举报

11

主题

80

帖子

199

积分

注册会员

Rank: 2

积分
199
发表于 2018-9-18 16:24:22 | 显示全部楼层
你正在运行Python 2,而不是Python 3.为了在Python 2中运行,请使用raw_input。
  1. input_variable = raw_input ("Enter your name: ")
  2. print ("your name is" + input_variable)
复制代码
回复

使用道具 举报

11

主题

63

帖子

159

积分

注册会员

Rank: 2

积分
159
发表于 2018-9-18 16:26:39 | 显示全部楼层
需要使用以下命令开始编写脚本:
#!/usr/bin/env python3
如果使用:
#!/usr/bin/env python
它将默认为Python 2.x. 如果没有任何内容以#开头,那么这些就会出现在脚本的第一行!(又名shebang)。
如果你的脚本以#! python开头
你可以把它改成:#! python3
虽然这种较短的格式只能被少数程序识别,例如启动器,所以它不是最佳选择。前两个示例使用得更为广泛,有助于确保您的代码可以在任何安装了Python的计算机上运行。
回复

使用道具 举报

10

主题

82

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
发表于 2018-9-18 16:27:47 | 显示全部楼层
你应该使用raw_input,因为你正在使用python-2.7,当你在一个变量例子上使用input()时:s = input('Name:'),它将执行Python Environement上的命令而不保存你在变量上写的内容(s)并创建错误如果你写的不是Definied,那么,你可以使用raw_input(),它会正确保存你在变量上写的内容,例如:f = raw_input('Name:')它不会在python环境中执行它而不创建任何可能的错误

  1. input_variable = raw_input('Enter Your Name : ')
  2. print("Your Name Is  : " + (input_variable))
复制代码

回复

使用道具 举报

4

主题

11

帖子

39

积分

新手上路

Rank: 1

积分
39
发表于 2018-9-18 16:29:04 | 显示全部楼层
你可以这样做:
  1. x = raw_input("enter your name")
  2. print "your name is %s " % x
复制代码

要么:
  1. x = str(input("enter your name"))
  2. print "your name is %s" % x
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表