index error: index out of range, It shouldnt be out of range though, i tried tracing it

index error: index out of range, It shouldnt be out of range though, i tried tracing it

这是一个用于加密消息的代码,就像您在 playfair 密码中所做的那样。

def encrypt(input_message):
    r1=r2=c1=c2=0
    play = playfair()
    listed_ip = list(input_message)
    j=1
    listed_op=list('#####################################################################################################################################################')
    for i in range(len(listed_ip)-1):
        first_letter = listed_ip[i]
        second_letter = listed_ip[j]
        if first_letter =='j':
            listed_ip[i] = 'i'
        if second_letter == 'j':
            listed_ip[j] = 'i'
        for row in range(5):
            for col in range(5):
                if play[row][col] ==first_letter:
                    r1=row
                    c1=col
                if play[row][col] == second_letter:
                    r2=row
                    c2=col
            if r1 == r2:
                listed_op[i] = play[r1][(c1+1)%5]
                listed_op[j] = play[r2][(c2 + 1) % 5]
            elif c1 ==c2:
                listed_op[i] = play[(r1+1)%5][c1]
                listed_op[j] = play[(r2+1)%5][c2]
            else:
                listed_op[i]=play[r1][c2]
                listed_op[j]=play[r2][c1]
            i+=2
            j+=2
    output_text = ''.join(listed_op)
    print(output_text)
encrypt('Hithisisvishal')

我应该得到一个输出来更改此输入消息,其中每个字母根据其在矩阵中的位置被另一个字母替换。

检查每个字母位置的矩阵的另一个函数是:

import numpy as np


def playfair():
    key = input('Enter the key :')
    key += 'abcdefghiklmnopqrstuvwxyz '
    size = len(key)
    listed_key=list(key)

    for i in range(size):
        if listed_key[i] == 'j':
            listed_key[i] = 'i'
        for j in range(i+1, size):
            if listed_key[j] == listed_key[i]:
                for k in range(j, size-1):
                    listed_key[k] = listed_key[k+1]
                size -= 1
            else : j += 1
    final_key = ''.join(listed_key)
    play = np.zeros((5, 5), 'U1')

    play.flat[:32] = list(final_key)
    return play

我得到的错误是:

Enter the keysecc

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-dd6ef9446893> in <module>
     33     output_text = ''.join(listed_op)
     34     print(output_text)
---> 35 encrypt('Hithisisvishal')

<ipython-input-6-dd6ef9446893> in encrypt(input_message)
      7     for i in range(len(listed_ip)-1):
      8         first_letter = listed_ip[i]
----> 9         second_letter = listed_ip[j]
     10         if first_letter =='j':
     11             listed_ip[i] = 'i'

IndexError: list index out of range

看来你的问题是你的 j 值对于数组来说太大了。当前,对于 listed_ip 中的每个元素,您的 j 值都增加了 10。按照这个速度,它只需要两次迭代就可以超过 listed_ip 的长度并抛出一个 IndexError。一个可能的解决方法是在 for 循环内而不是之前将 j 值设置为 1。这样对于 listed_ip 中的每个值,您都从 j=1 开始。

这是我的解决方法:

def encrypt(input_message):
    r1=r2=c1=c2=0
    play = playfair()
    listed_ip = list(input_message)
    print(listed_ip)
    
    listed_op=list('#####################################################################################################################################################')
    for i in range(len(listed_ip)-1):
        j=1
        first_letter = listed_ip[i]
        second_letter = listed_ip[j]
        if first_letter =='j':
            listed_ip[i] = 'i'
        if second_letter == 'j':
            listed_ip[j] = 'i'
        for row in range(5):
            for col in range(5):
                if play[row][col] ==first_letter:
                    r1=row
                    c1=col
                if play[row][col] == second_letter:
                    r2=row
                    c2=col
            if r1 == r2:
                listed_op[i] = play[r1][(c1+1)%5]
                listed_op[j] = play[r2][(c2 + 1) % 5]
            elif c1 ==c2:
                listed_op[i] = play[(r1+1)%5][c1]
                listed_op[j] = play[(r2+1)%5][c2]
            else:
                listed_op[i]=play[r1][c2]
                listed_op[j]=play[r2][c1]
            i+=2
            j+=2
    output_text = ''.join(listed_op)
    print(output_text)
encrypt('Hithisisvishal')

这只是对那一行的更改,看看它是否适合您

您的 i 和 j 分配不正确。

for i in range(len(listed_ip)-1):

变量i会是0,1,2,... 你设置i+=2后,循环回来的时候会重新设置。变量 j 在第二个 for 循环中更新。因此,您有索引错误。

def encrypt(input_message):
    r1 = r2 = c1 = c2 = 0
    play = playfair()
    listed_ip = list(input_message)
    j = 1
    listed_op = list('#'*150)

    for i in range(0, len(listed_ip)-1, 2):
        j = i + 1
        first_letter = listed_ip[i]
        second_letter = listed_ip[j]
        if first_letter == 'j':
            listed_ip[i] = 'i'
        if second_letter == 'j':
            listed_ip[j] = 'i'
        for row in range(5):
            for col in range(5):
                if play[row][col] == first_letter:
                    r1 = row
                    c1 = col
                if play[row][col] == second_letter:
                    r2 = row
                    c2 = col
            if r1 == r2:
                listed_op[i] = play[r1][(c1+1) % 5]
                listed_op[j] = play[r2][(c2 + 1) % 5]
            elif c1 == c2:
                listed_op[i] = play[(r1+1) % 5][c1]
                listed_op[j] = play[(r2+1) % 5][c2]
            else:
                listed_op[i] = play[r1][c2]
                listed_op[j] = play[r2][c1]
            # i += 2
            # j += 2
    output_text = ''.join(listed_op)
    print(output_text)