获取错误列表对象在此凯撒密码上不可调用

时间:2017-06-17 04:31:16

标签: python python-3.x

plaintext =[
        "this is a test",
        "caesar’s wife must be above suspicion",
        "as shatner would say: you, should, also, be, able, to, handle, punctuation.",
        "to mimic chris walken: 3, 2, 1, why must you, pause, in strange places?",]

shift = 3
def caesar(plaintext):


    alphabet=["a","b","c","d","e","f","g","h","i","j","k","l",
    "m","n","o","p","q","r","s","t","u","v","w","x","y","z"]


    dic={}
    for i in range(0,len
                       (alphabet)):
        dic[alphabet[i]]=alphabet[(i+shift)%len(alphabet)]


    ciphertext=""
    for l in plaintext():
        if l in dic:
            l=dic[l]
        ciphertext+=l

    return ciphertext



print [caesar(plaintext)]

我不确定为什么它会给我这个错误。我需要一些帮助。我尝试将括号括起来并替换了parathesis,但它仍然给出了错误。

Traceback (most recent call last):
  File "C:/Users/iii/Desktop/y.py", line 33, in <module>
    print (caesar(plaintext))
  File "C:/Users/iii/Desktop/y.py", line 24, in caesar
    for l in plaintext():
TypeError: 'list' object is not callable

1 个答案:

答案 0 :(得分:0)

  1. 首先,print(caesar(plaintext))是如何在Python 3.x中打印。
  2. 您也无法在不调用len()的情况下遍历列表:for l in range(len(plaintext)):
  3. 您也无法增加字符串,因此我建议您是否确实要将变量ciphertext添加1,请将其设为数字​​:ciphertext=0
  4. 这个修改过的代码运行没有错误,只要它仍然是你想要的,我无法确定它是否。

    plaintext =[
            "this is a test",
            "caesar’s wife must be above suspicion",
            "as shatner would say: you, should, also, be, able, to, handle, punctuation.",
            "to mimic chris walken: 3, 2, 1, why must you, pause, in strange places?",]
    
    shift = 3
    def caesar(plaintext):
        alphabet=["a","b","c","d","e","f","g","h","i","j","k","l",
        "m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    
        dic={}
        for i in range(0,len(alphabet)):
            dic[alphabet[i]]=alphabet[(i+shift)%len(alphabet)]
    
        ciphertext=0
        for l in range(len(plaintext)):
            if l in dic:
                l=dic[l]
            ciphertext+=l
    
        return ciphertext
    
    print(caesar(plaintext))
    

    <强> 修改

    如果你想制作一个Caesar密码,this web site可以很好地解释如何用Python编写它。