我正在创建一个应该加密你传入的密码的程序。但是,每当我运行它时,都会引发错误:
Password = float(input('Enter Password: '))
ValueError:无法将字符串转换为float:' Banana' (我为这个测试选择了这个词)
这是我的代码:
#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")
答案 0 :(得分:0)
在Python 3.x中,input
将返回一个字符串。您可以在the PyDocs中了解相关信息。当你给Python输入Banana
时,它很困惑。您无法将Banana
转换为float
类型。如果您正在考虑将字符串转换为程序的一组索引,您可以尝试这一点(请注意我添加并在代码中实现的3个新函数):
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")
当然,这是一种弱加密,如果你想要更好的加密(专业人士使用的东西),请看看pycrypto。