为什么 Python 3 中的`input` 抛出 NameError: name... is not defined

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16625669/
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-08-18 23:11:13  来源:igfitidea点击:

Why is `input` in Python 3 throwing NameError: name... is not defined

pythonpython-3.xstringinputeval

提问by DenisAnanev

I have a string variable test, in Python 2.x this works fine.

我有一个字符串变量test,在 Python 2.x 中这工作正常。

test = raw_input("enter the test") 
print test

But in Python 3.x, I do:

但在 Python 3.x 中,我这样做:

test = input("enter the test") 
print test

with the input string sdas, and I get an error message

使用输入字符串sdas,我收到一条错误消息

Traceback (most recent call last):
 File "/home/ananiev/PycharmProjects/PigLatin/main.py", line 5, in <module>
    test = input("enter the test")
 File "<string>", line 1, in <module> 
NameError: name 'sdas' is not defined

回答by Noctua

I'd say the code you need is:

我会说你需要的代码是:

test = input("enter the test")
print(test)

Otherwise it shouldn't run at all, due to a syntax error. The printfunction requires brackets in python 3. I cannot reproduce your error, though. Are you sure it's those lines causing that error?

否则,由于语法错误,它根本不应该运行。该print函数需要在 python 3 中使用括号。不过,我无法重现您的错误。你确定是那些行导致了那个错误?

回答by Cairnarvon

You're running your Python 3 code with a Python 2 interpreter. If you weren't, your printstatement would throw up a SyntaxErrorbefore it ever prompted you for input.

您正在使用 Python 2 解释器运行 Python 3 代码。如果不是,您的print语句会SyntaxError在提示您输入之前抛出一个。

The result is that you're using Python 2's input, which tries to evalyour input (presumably sdas), finds that it's invalid Python, and dies.

结果是您正在使用 Python 2 的input,它尝试eval输入(大概sdas),发现它是无效的 Python,然后死亡。

回答by none

sdas is being read as a variable. To input a string you need " "

sdas 被读取为变量。要输入字符串,您需要“”

回答by Kevin Wright

I got the same error. In the terminal when I typed "python filename.py", with this command, python2 was tring to run python3 code, because the is written python3. It runs correctly when I type "python3 filename.py" in the terminal. I hope this works for you too.

我得到了同样的错误。在终端中,当我输入“python filename.py”时,用这个命令,python2 正在尝试运行 python3 代码,因为它是写成 python3.py 的。当我在终端中输入“python3 filename.py”时它运行正常。我希望这也适用于你。

回答by Menuka Ishan

In operating systems like Ubuntu python comes preinstalled. So the default version is python 2.7 you can confirm the version by typing below command in your terminal

在像 Ubuntu 这样的操作系统中,预装了 python。所以默认版本是 python 2.7 你可以通过在终端中输入以下命令来确认版本

python -V

if you installed it but didn't set default version you will see

如果你安装了它但没有设置默认版本,你会看到

python 2.7

in terminal. I will tell you how to set the default python version in Ubuntu.

在终端。我会告诉你如何在 Ubuntu 中设置默认的 python 版本。

A simple safe way would be to use an alias. Place this into ~/.bashrcor ~/.bash_aliasesfile:

一个简单安全的方法是使用别名。将其放入~/.bashrc~/.bash_aliases文件中:

alias python=python3

After adding the above in the file, run the command below:

在文件中添加以上内容后,运行以下命令:

source ~/.bash_aliasesor source ~/.bashrc

source ~/.bash_aliases或者 source ~/.bashrc

now check python version again using python -V

现在使用再次检查python版本 python -V

if python version 3.x.x one, then the error is in your syntax like using printwith parenthesis. change it to

如果 python 版本为 3.xx 一,那么错误就在你的语法中,比如使用带括号的打印。将其更改为

test = input("enter the test")
print(test)

回答by Mayank Jain

If we setaside the syntax error of print, then the way to use input in multiple scenarios are -

如果抛开print的语法错误,那么多种场景下使用input的方式是——

If using python 2.x :

如果使用 python 2.x :

then for evaluated input use "input"
example: number = input("enter a number")

and for string use "raw_input"
example: name = raw_input("enter your name")

If using python 3.x :

如果使用 python 3.x :

then for evaluated result use "eval" and "input"
example: number = eval(input("enter a number"))

for string use "input"
example: name = input("enter your name")

回答by s3n0

temperature = input("What's the current temperature in your city? (please use the format ??C or ???F) >>> ")

### warning... the result from input will <str> on Python 3.x only
### in the case of Python 2.x, the result from input is the variable type <int>
### for the <str> type as the result for Python 2.x it's neccessary to use the another: raw_input()

temp_int = int(temperature[:-1])     # 25 <int> (as example)
temp_str = temperature[-1:]          # "C" <str> (as example)

if temp_str.lower() == 'c':
    print("Your temperature in Fahrenheit is: {}".format(  (9/5 * temp_int) + 32      )  )
elif temp_str.lower() == 'f':
    print("Your temperature in Celsius is: {}".format(     ((5/9) * (temp_int - 32))  )  )