Python 函数和方法以及str或return问题
因此,我有一个程序,需要一个描述,密码和密钥。但是,当我将密码和密钥放入加密方法时,由于它是一个函数,当我尝试获取返回时会出现错误,那么我该如何解决这个问题呢Python 函数和方法以及str或return问题,python,python-2.7,python-3.x,encryption,Python,Python 2.7,Python 3.x,Encryption,因此,我有一个程序,需要一个描述,密码和密钥。但是,当我将密码和密钥放入加密方法时,由于它是一个函数,当我尝试获取返回时会出现错误,那么我该如何解决这个问题呢 def encrypt(plaintext, key): alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-" cipher = " " for c in plaintext: if c in alph
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
您处理函数使用不当:
encrypt(entry2, entry3)
entry2 = encrypt
在第一行中,调用entry2
和entry3
上的encrypt
函数,它返回结果,然后立即丢弃,因为您没有将其分配给任何对象
然后在第二行中,将entry2
设置为函数encrypt
,而不是上一次函数调用的结果。因此,您试图连接一个字符串和一个没有等效字符串的函数
相反,将encrypt
调用的结果分配给变量,并在write
调用中使用该结果
例如:
result = encrypt(entry1, entry2)
代码片段中的缩进被弄乱了。每个函数定义中都没有任何内容。你能修好压痕吗?