所以我有一个程序,它带有描述,密码和密钥。但是当我把密码和密钥放入加密方法时,由于它是一个函数,当我试图获得返回时会出现错误,所以我该如何解决这个问题?
def encrypt(plaintext, key):
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-"
cipher = " "
for c in plaintext:
if c in alphabet:
cipher += alphabet[(alphabet.index(c) + key) % (len(alphabet))]
print("Your encrypeted msg is: ", cipher)
return cipher
def decrypt(cryptedtext, key):
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-"
cipher = " "
for c in cryptedtext:
if c in alphabet:
cipher += alphabet[(alphabet.index(c) - key) % len(alphabet)]
print("decrypt: ", cipher)
def EnterInfo():
entry = input(" please enter in des to be stored")
entry2 = input(" please enter in pass to be stored")
entry3 = int(input(" please enter in key to be stored"))
encrypt(entry2, entry3)
entry2 = encrypt
with open("test.txt", 'a') as myfile:
myfile.write(entry + ":" + entry2 + ":\n")
EnterInfo()
错误:
File "C:/Users/yoyo/PycharmProjects/crypt/CombineCryptAndTxtPyFile.py", line 29, in EnterInfo
myfile.write(entry + ":" + entry2 + ":\n")
TypeError: Can't convert 'function' object to str implicitly
答案 0 :(得分:1)
您错误处理了功能使用情况:
encrypt(entry2, entry3)
entry2 = encrypt
在第一行,您调用encrypt
和entry2
上的entry3
函数,然后返回结果,然后立即丢弃,因为您还没有分配它任何事情。
然后在第二行中,将entry2
设置为等于函数 encrypt
,而不是之前函数调用的结果。所以你试图连接一个字符串和一个没有字符串等效的函数。
相反,请将encrypt
调用的结果分配给变量,并在write
调用中使用该结果。
示例:
result = encrypt(entry1, entry2)