Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数和方法以及str或return问题_Python_Python 2.7_Python 3.x_Encryption - Fatal编程技术网

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)

代码片段中的缩进被弄乱了。每个函数定义中都没有任何内容。你能修好压痕吗?