我一直在python TypeError中收到错误消息:' str'对象不可调用

时间:2016-02-24 21:59:01

标签: python

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

  

TypeError:' str'对象不可调用

我的代码目前是:

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)

如果有人能

,请感谢他们的帮助

1 个答案:

答案 0 :(得分:3)

您的字符串cipher(第3行中的违规)与您的函数cipher具有相同的名称:

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

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

当您拨打此支票时cipher输入string,请更改您的字符串名称或功能名称。