当我试图运行这个简单的脚本时,我得到一个错误:

input_variable = input("Enter your name: ")
print("your name is" + input_variable)

假设我输入“dude”,我得到的错误是:

  line 1, in <module>
    input_variable = input("Enter your name: ")
  File "<string>", line 1, in <module>
NameError: name 'dude' is not defined

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


当前回答

我们正在使用以下代码,python 2和python 3都可以使用

#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Enter your name: "))

其他回答

下面是一个与Python 2.7和Python 3+兼容的输入函数: (稍微修改了@Hardian的回答)以避免UnboundLocalError:在赋值错误之前引用本地变量“input”

def input_compatible(prompt=None):
    try:
        input_func = raw_input
    except NameError:
        input_func = input
    return input_func(prompt)

这里还有另一个没有try块的选项:

def input_compatible(prompt=None):
    input_func = raw_input if "raw_input" in __builtins__.__dict__ else input
    return input_func(prompt)
input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

输入时必须使用单引号或双引号

Ex:'dude' -> correct

    dude -> not correct

你可以这样做:

x = raw_input("enter your name")
print "your name is %s " % x

or:

x = str(input("enter your name"))
print "your name is %s" % x

对于任何可能遇到这个问题的人来说,事实证明,即使您包含#!/usr/bin/env python3在脚本的开头,如果文件不可执行,shebang将被忽略。

要确定文件是否可执行:

在命令行中运行。/filename.py 如果-bash: ./filename.py: Permission denied,执行chmod a+x filename.py 重新运行。/filename.py

如果你已经包含了import sys;print(sys.version),就像Kevin建议的那样,你现在会看到脚本正在被python3解释

博士TL;

Python 2.7中的input函数,作为Python表达式计算你输入的任何内容。如果你只是想读取字符串,那么使用Python 2.7中的raw_input函数,它不会计算读取的字符串。

如果你使用的是Python 3。X, raw_input已重命名为input。引用Python 3.0发布说明,

Raw_input()重命名为input()。也就是说,新的input()函数从sys. js中读取一行。Stdin并返回它,并去掉尾随换行符。如果输入提前终止,则引发EOFError。要获得input()的旧行为,请使用eval(input())


在Python 2.7中,有两个函数可用于接受用户输入。一个是input,另一个是raw_input。你可以这样想它们之间的关系

input = eval(raw_input)

考虑下面的代码段,以更好地理解这一点

>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'

input接收来自用户的字符串,并在当前Python上下文中计算该字符串。当我输入dude作为输入时,它发现dude被绑定到值thefourtheye,所以求值的结果就变成了thefourtheye,它被赋值给input_variable。

如果我输入当前python上下文中不存在的其他内容,NameError将失败。

>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined

Python 2.7输入的安全注意事项:

因为无论评估的是哪种用户类型,都会带来安全问题。例如,如果你已经用import os在程序中加载了os模块,然后用户输入

os.remove("/etc/hosts")

这将被python作为函数调用表达式计算,并将被执行。如果您正在以提升的权限执行Python, /etc/hosts文件将被删除。看,这有多危险?

为了演示这一点,让我们再次尝试执行输入函数。

>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude

现在,当输入("Enter your name: ")被执行时,它会等待用户输入,而用户输入是有效的Python函数调用,因此也会调用该函数。这就是为什么我们看到再次输入您的名字:再次提示。

你最好使用raw_input函数,像这样

input_variable = raw_input("Enter your name: ")

如果需要将结果转换为其他类型,则可以使用适当的函数转换raw_input返回的字符串。例如,要将输入读取为整数,请使用int函数,如此答案所示。

在python中3。x中,只有一个函数来获取用户输入,叫做input,相当于Python 2.7的raw_input。