“int”对象不可下标(函数调用错误)

2024-05-19 15:06:00 发布

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

我试图在不知道密钥的情况下破解一个vigenere密码,我正在努力找出我的错误,因为caesar_break函数本身似乎工作得很好

这是我试图调用的函数的代码:

def caesar_break( cipher, frequencies ): # help!
    alpha = ["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" ]
    dist_ls = []

    for key in alpha:
        poss_plaintxt = caesar_dec( cipher, key )
        counts = letter_counts( poss_plaintxt )
        observed = normalize( counts )
        dist = distance( observed, frequencies )
        dist_ls.append( (dist , poss_plaintxt , key) )
        can = dist_ls[ 0 ][ 0 ]
        can_t = 0
        for t in dist_ls:
            if t[ 0 ] < can:
                can = t[ 0 ]
                can_t = t

    return [ can_t[ 2 ], can_t[ 1 ] ]

这就是我目前的功能,我还没有完全完成,但我只需要找出这个错误,以便继续:

def vig_break( c, maxlen, frequencies ):
    from i in range( 1, maxlen ):
        break_ls = list(vig_break_for_length( c, i, frequencies ))

    print( break_ls ) #this print is unneeded, i just like to test my code as i go

具体而言,这是错误代码:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ciphers.py", line 310, in vig_break
    break_ls = list(vig_break_for_length( c, i, frequencies ))
  File "ciphers.py", line 288, in vig_break_for_length
    split_break = caesar_break( ciphertext, frequencies )
  File "ciphers.py", line 192, in caesar_break
    return [ can_t[ 2 ], can_t[ 1 ] ]
    TypeError: 'int' object is not subscriptable

Tags: keyinfordistlinecanlsfile