Python NameError: name '[string]' 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38857541/
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
NameError: name '[string]' is not defined
提问by CodyT96
When testing my program, I keep getting this error:
在测试我的程序时,我不断收到此错误:
1. Encrypt a file
2. Decrypt a file
----> 1
Enter the filename you'd like to encrypt: test
Traceback (most recent call last):
File "./encrypt.py", line 71, in <module>
Main()
File "./encrypt.py", line 58, in Main
filename = input("Enter the filename you'd like to encrypt: ")
File "<string>", line 1, in <module>
NameError: name 'test' is not defined
And here is my code for the Main() function:
这是我的 Main() 函数代码:
def Main():
print("1. Encrypt a file")
print("2. Decrypt a file")
choice = str(input("----> "))
if choice == '1':
filename = input("Enter the filename you'd like to encrypt: ")
password = input("Enter a password used for the encryption tool: ")
encrypt(getKey(password), filename)
print("File has been encrypted.")
elif choice == '2':
filename = input("Enter the filename you'd like to decrypt: ")
password = input("Enter the password used for the encryption of this file: ")
decrypt(getKey(password), filename)
print("File has been decrypted. Note that if the password used in the encryption does " \
+ "not match the password you entered in, the file will remain encrypted.")
else:
print("Invalid option. Closing the program...")
I am using the simple input() method to get my data ('test', for example), and it keeps telling me whatever information I enter in at runtime, the name of what I just entered is not defined. I don't see any formatting errors, syntax errors, etc.
我正在使用简单的 input() 方法来获取我的数据(例如,'test'),它不断告诉我我在运行时输入的任何信息,我刚刚输入的名称没有定义。我没有看到任何格式错误、语法错误等。
回答by Peter Wang
You're going to want to use raw_input()
instead of input()
. input
tries to run the expression it gets as a Python expression, whereas raw_input
returns a string. This is in Python 2.x; in 3.x raw_input
doesn't exist.
你会想要使用raw_input()
而不是input()
. input
尝试将它得到的表达式作为 Python 表达式运行,而raw_input
返回一个字符串。这是在 Python 2.x 中;在 3.xraw_input
中不存在。
When you get the NameError
, it's trying to run your input as an expression, but test
doesn't exist.
当您获得 时NameError
,它会尝试将您的输入作为表达式运行,但test
并不存在。
回答by CodyT96
It turns out my linux distro has both python 2.7.12 and python 3.5.2. Apparently the system defaults to python 2.7.12 instead of the newer version, so I fixed it by changing:
原来我的 linux 发行版有 python 2.7.12 和 python 3.5.2。显然系统默认为 python 2.7.12 而不是较新的版本,所以我通过更改来修复它:
#!/usr/bin/python
to:
到:
#!/usr/bin/python3
回答by dietbacon
The input
function treats whatever you input as a python expression. What you're looking for is the raw_input
function which treats your input as a string.
Switch all your input
s for raw_input
s and you should be fine.
该input
函数将您输入的任何内容视为 python 表达式。您正在寻找的是raw_input
将您的输入视为字符串的函数。把你所有的input
s 换成raw_input
s ,你应该没问题。
回答by UltraInstinct
it keeps telling me whatever information I enter in at runtime, the name of what I just entered is not defined. I don't see any formatting errors, syntax errors, etc.
它一直告诉我我在运行时输入的任何信息,我刚刚输入的名称没有定义。我没有看到任何格式错误、语法错误等。
You are probably using Python 2.x. You should use: raw_input(..)
instead of input(..)
您可能正在使用 Python 2.x。你应该使用:raw_input(..)
而不是input(..)