我一直在python TypeError中得到错误消息:“str”对象不是callab

2024-04-25 05:55:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个密码,收到了一条错误消息:

TypeError: 'str' object is not callable

我目前的代码是:

alphabet = "abcdefghijklmnopqrstuvwxyz"
key = 5
cipher = ' ' 

choice = input('Do you wish to encrypt or decrypt? E/D')
if not choice == 'D' and not choice == 'E':
    raise Exception('Must enter E or D for Encrypt or Decrypt')
user_input = input('Please enter your word')
cipher2 = cipher (user_input, alphabet, choice, key)
print('your encrypted mesaage 2 is: ' + cipher2)

#Method can encrypt or decrypt the entered word
def cipher(plain_text, alphabet, choice, key):
    cipher = ''
    for c in plain_text:
        if c in alphabet:
            if choice == 'E':
                val1 = alphabet.index(c)
                val2 = key
                cipher += alphabet[ (val1 + val2) % (len(alphabet))]
            elif choice == 'D':
                cipher = cipher + alphabet[ (alphabet.index(c)-key) % (len(alphabet))]
return cipher

错误在第九行:

    cipher2 = cipher (user_input, alphabet, choice, key)

如果有人能帮忙,我会很感激的


Tags: orkeyinputifis错误notencrypt
1条回答
网友
1楼 · 发布于 2024-04-25 05:55:45

字符串cipher(在第3行中定义)与函数cipher具有相同的名称:

print (type(cipher)) #  class <'str'>

cipher2 = cipher (user_input, alphabet, choice, key) # cipher is string here not function

当您调用这个check cipher类型时,它是string,所以请更改您的字符串名或函数名。你知道吗

相关问题 更多 >