所以我有一个程序,需要一个描述,密码和密钥。但是当我将密码和密钥放入encrypt方法时,由于它是一个函数,当我尝试获取返回时会出现错误,那么我该如何绕过这个问题呢?你知道吗
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
你对函数用法处理不当:
在第一行中,调用
entry2
和entry3
上的encrypt
函数,它返回结果,然后立即丢弃,因为您没有将它赋给任何对象。你知道吗然后在第二行中,将
entry2
设置为函数encrypt
,而不是上一个函数调用的结果。因此,您试图连接一个字符串和一个没有等效字符串的函数。你知道吗相反,将
encrypt
调用的结果赋给一个变量,并在write
调用中使用该结果。你知道吗示例:
相关问题 更多 >
编程相关推荐