Posted on:

15 May 2021

0

Error in coding for Caesar cipher solution

Following code surprisingly returned an error, please help me let me know whats wrong with the code and suggest correction. I am trying to implement Caesar Cipher encryption of a given input string using this code

def CC_Encrypt(string, offset):
    encrypted_str = []
    print (string)
    string_len = len (string)
    for index in range(string_len):
        if ((index + offset) < string_len):
            encrypted_str [index + offset] = string[index]
        else:
            encrypted_str [index - offset] = string[index]
string = input()
CC_Encrypt(string, 3)



Output showed following error:

test
test


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-1515a4e1ff91> in <module>
     18 
     19 string = input()
---> 20 CC_Encrypt(string, 3)

<ipython-input-2-1515a4e1ff91> in CC_Encrypt(string, offset)
     11 #             encrypted_str [index + offset] = string[index]
     12         if ((index + offset) < string_len):
---> 13             encrypted_str [index + offset] = string[index]
     14         else:
     15             encrypted_str [index - offset] = string[index]

IndexError: list assignment index out of range
0 answers ( 0 marked as helpful)

Submit an answer