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:
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.
4 Answers
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.
When you get the NameError, it's trying to run your input as an expression, but test doesn't exist.
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:
#!/usr/bin/python
to:
#!/usr/bin/python3
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(..)
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 inputs for raw_inputs and you should be fine.