2016-05-10 2 views
-6

ich ein Programm mache, die das Passwort, das Sie in geben verschlüsseln sollte jedoch, wenn ich es laufen, es löst einen Fehler.Verschlüsselungs- und Entschlüsselungs ein Passwort werfen Valueerror

Password = float(input('Enter Password: ')) 

ValueError: could not convert string to float: 'Banana' (the word I chose for this test)

Hier ist mein code:

#Macchiat0 
#10 May 2016 
#This program will encrypt and decrypt user passwords. 

#init 
encryptionlist = (('a','q'), 
        ('b','w'), 
        ('c','e'), 
        ('d','r'), 
        ('e','t'), 
        ('f','y'), 
        ('g','u'), 
        ('h','i'), 
        ('i','o'), 
        ('j','p'), 
        ('k','a'), 
        ('l','s'), 
        ('m','d'), 
        ('n','f'), 
        ('o','g'), 
        ('p','h'), 
        ('q','j'), 
        ('r','k'), 
        ('s','l'), 
        ('t','z'), 
        ('u','x'), 
        ('v','c'), 
        ('w','v'), 
        ('x','b'), 
        ('y','n'), 
        ('z','m')) 

print('This program will encrypt and decrypt user passwords') 

#Program Menu 
ans = True 

while True: 
    print('1. Enter 1 to encrypt a password: ') 
    print('2. Enter 2 to decrypt a password: ') 
    print('3. Exit/Quit') 
    ans = input('What do you want to do? ') 

    if ans == "1": 
     print("\n Enter 1 to encrypt a password: ") 

     Password = float(input('Enter Password: ')) 
     print('Your new encryptid password is:', Password) 
    if ans == "2": 
     print("\n Enter 2 to decrypt a password: ") 

     Password = float(input('Enter Password: ')) 
     print('Your new decrypted password is:', Password) 
    elif ans == "3": 
     print("\n Goodbye") 
     break 
    else: 
     print("\n Not Valid Choice Try Again") 
+5

Der Fehler ist sehr klar: 'ValueError: String konnte nicht in Float konvertiert werden: 'Banana'' Sie können' Banana' nicht in einen Float konvertieren. – DeepSpace

+7

Die Antwort kommt direkt aus der Fehlermeldung,' banana 'ist keine Nummer. Warum benutzen Sie 'float' in' Password = float (Eingabe ('Enter Password:')) ' – roganjosh

+1

Es ist auch sehr falsch, Benutzerpasswörter entschlüsseln zu können. Aber wenn es zum Spaß ist, dann ist es in Ordnung. Was Ihr Problem angeht, müssen Sie, wie andere sagten, das 'float (...)' löschen. – DeltaWeb

Antwort

0

In Python 3.x gibt input eine Zeichenfolge zurück. Sie können darüber in the PyDocs lesen. Wenn Sie Python die Eingabe Banana gaben, ist es verwirrt. Sie können Banana nicht in einen float Typ konvertieren. Wenn Sie bei der Umwandlung der Zeichenfolge in einen Satz von Indizes für Ihr Programm suchen, können Sie versuchen, diese (die 3 neue Funktionen beachten Sie, ich hinzugefügt und in den Code implementiert):

encryptionlist = (('a','q'), 
        ('b','w'), 
        ('c','e'), 
        ('d','r'), 
        ('e','t'), 
        ('f','y'), 
        ('g','u'), 
        ('h','i'), 
        ('i','o'), 
        ('j','p'), 
        ('k','a'), 
        ('l','s'), 
        ('m','d'), 
        ('n','f'), 
        ('o','g'), 
        ('p','h'), 
        ('q','j'), 
        ('r','k'), 
        ('s','l'), 
        ('t','z'), 
        ('u','x'), 
        ('v','c'), 
        ('w','v'), 
        ('x','b'), 
        ('y','n'), 
        ('z','m')) 

print('This program will encrypt and decrypt user passwords') 

def letter_index(letter): 
    return ord(letter) - 97 

def encrypt(text): 
    lowered_text = text.lower() 
    encrypted_text = [letter_index(x) for x in lowered_text] 
    encrypted_text = "".join([encryptionlist[x][1] for x in encrypted_text]) 
    return encrypted_text 

def decrypt(text): 
    lowered_text = text.lower() 
    # the decryption process will yield worst case speed of O(n) 
    # if you were to loop through the whole thing. 
    # A faster way to sort it by the value e.g. 
    sorted_encryptionlist = sorted(encryptionlist, key=lambda x: x[1]) 
    decrypted_text = [letter_index(x) for x in lowered_text] 
    decrypted_text = "".join([sorted_encryptionlist[x][0] for x in decrypted_text]) 
    return decrypted_text 


#Program Menu 
ans = True 

while True: 
    # Get user input 
    print('1. Enter 1 to encrypt a password: ') 
    print('2. Enter 2 to decrypt a password: ') 
    print('3. Exit/Quit') 
    ans = input('What do you want to do? ') 

    if ans == "1": 
     print("\nEnter 1 to encrypt a password: ") 

     Password = input('Enter Password: ') 

     print('Your new encryptid password is:', encrypt(Password)) 
    if ans == "2": 
     print("\nEnter 2 to decrypt a password: ") 

     Password = input('Enter Password: ') 

     print('Your new decrypted password is:', decrypt(Password)) 
    elif ans == "3": 
     print("\nGoodbye") 
     break 
    else: 
     print("\nNot Valid Choice Try Again") 

Natürlich ist dies ein schwach Verschlüsselung, wenn Sie eine bessere Verschlüsselung wünschen (das Zeug von Profis verwendet), werfen Sie einen Blick auf pycrypto.

Verwandte Themen