2016-08-09 6 views
1

definiert Wenn mein Programm zu testen, halte ich immer diese Fehlermeldung:Nameerror: name '[string]' ist nicht

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 

Und hier ist mein Code für die Main() Funktion:

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...") 

Ich verwende die einfache Methode input(), um meine Daten zu erhalten (zum Beispiel 'test'), und sie teilt mir ständig die Informationen mit, die ich zur Laufzeit eingegeben habe. Der Name der gerade eingegebenen Daten ist nicht definiert. Formatierungsfehler, Syntaxfehler usw. werden nicht angezeigt.

+0

Welches Python-Version ist das? – Mureinik

+3

Mögliches Duplikat von [unerwartetes Python-EOF beim Parsen] (http://stackoverflow.com/questions/5074225/python-unexpected-eof-while-parsing) –

+0

Ich weiß, dass das wie ein komischer Betrogener aussieht, aber ich vermute stark, dass Sie verwenden 'input' anstelle von' raw_input' in Python 2 und die oberste Antwort dort ist die klarste Erklärung, die ich finden kann. –

Antwort

0

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.

Sie verwenden wahrscheinlich Python 2.x. Sie sollten verwenden: raw_input(..) anstelle von input(..)

1

Sie werden raw_input() anstelle von input() verwenden möchten. input versucht, den Ausdruck auszuführen, der als Python-Ausdruck abgerufen wird, während raw_input eine Zeichenfolge zurückgibt. Dies ist in Python 2.x; in 3.x raw_input existiert nicht.

Wenn Sie die NameError erhalten, versucht es, Ihre Eingabe als Ausdruck auszuführen, aber test ist nicht vorhanden.

0

Die Funktion input behandelt alles, was Sie als Python-Ausdruck eingeben. Was Sie suchen, ist die raw_input-Funktion, die Ihre Eingabe als Zeichenfolge behandelt. Schalten Sie alle Ihre input s für raw_input s und Sie sollten in Ordnung sein.

1

Es stellt sich heraus, dass meine Linux Distribution Python 2.7.12 und Python 3.5.2 hat. Offenbar das System standardmäßig Python 2.7.12 anstelle der neueren Version, so fixierte ich es durch eine Änderung:

#!/usr/bin/python 

zu:

#!/usr/bin/python3 
Verwandte Themen