indexError:尝试加密文本时列出索引超出范围

时间:2017-03-13 02:10:27

标签: python list indexing error-handling

由于某些原因,我的代码导致" IndexError:列表索引超出范围"当输入书籍旅行者银河系指南的明文和“嘿嘿”的关键时。我不知道是什么导致这种情况,非常感谢任何帮助!

这是我的代码:

def vigenere_enc( plain, key ):

    upper_plain = simplify_string( plain )
    plain_ls = list( upper_plain )
    key_ls = list( key )
    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" ]
    ls = []

    for let, key in zip(plain, cycle(key)):
        shift_location =  (alpha.index( key )) + (alpha.index( let ))

        if shift_location > 26:

            shift_from_beg = ( shift_location - 26)
            add_let = alpha[ shift_from_beg ]
            ls.append( add_let )

        else:

            else_add_let = alpha[ shift_location ]
            ls.append( else_add_let )

    string = ''.join( ls )
    return string

0 个答案:

没有答案