Help Resolving Error With Vigenere Cipher Code

Link for Project Coded Correspondence:
https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication

I managed to get the Caesar cipher working, and have been working to adapt the code for the vigenere cipher.

My code to shift letter index, to the proper index is as follows:

def vigenere_decrypt(message, key):
decrypted_message = “”
key_index = 0
for c in message:
if c in vigenere_alphabet:
# # This needs to find the index of key_index at alphabet, value for index of this one. Original Index +
# index of key ##
decrypted_message += vigenere_alphabet[((vigenere_alphabet.index© + key.index(key_index)) % (len(vigenere_alphabet)))]

        ## Rotate Key Forward ##
        key_index =+ 1
        if key_index == len(key):
            key_index = 0

    elif c in alphabet:
        decrypted_message += alphabet[((alphabet.index(c) + key.index(key_index)) % (len(alphabet)))]

        ## Rotate forward in Key##
        key_index += 1
        if key_index == len(key):
            key_index = 0

    else:
        decrypted_message += c

return decrypted_message

What I am trying to do is to take the character in the message, and add to the empty decrypted_message string the character that has shifted index from, its index of index c and moving it up to the index of my key where it is currently indexed modulo length of alphabet to handle the wrap, if applicable.

There doesn’t seem to be an error with the first library, but my lowercase library “alphabet” appears to be hanging up the code.

When run though i get:

Traceback (most recent call last):
File "*, line 118, in
print(vigenere_decrypt(vigenere_cipher_message, vigenere_cipher_keyword))
File "/
, line 103, in vigenere_decrypt
decrypted_message += alphabet[((alphabet.index© + key.index(key_index)) % (len(alphabet)))]
TypeError: must be str, not int

I’m trying to figure out why I’m getting getting this error, even though I’m using pretty much the same strategy as I did in the previous exercise, just a more complex way of setting the alphabet.

Thanks in advance for taking a look.

It’s not really clear which expression is causing the issue, especially since alphabet isn’t included in the code you provided. At some point an integer was passed when a string was required. You may have to check the type associated with the names you’re using if you can’t narrow it down from the error message alone.

It would be easier to check if you wrapped your code with triple backticks for formatting like so (more details on formatting at this FAQ-
` ` `
code goes here
` ` `

Sorry, there is a lot of code from above where I did the other part of the challenge, here is alphabet, and vigenere alphabet:

alphabet = (“abcdefghijklmnopqrstuvwxyz”)
vigenere_alphabet = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”

I will attempt to format the code as you suggested.

Thank you for taking a look.

1 Like
def vigenere_decrypt(message, key):
    decrypted_message = ""
    key_index = 0
    for c in message:
        if c in vigenere_alphabet:
            # # This needs to find the index of key_index at alphabet, value for index of this one.  Original Index +
            # index of key ##
            decrypted_message += vigenere_alphabet[((vigenere_alphabet.index(c) + key.index(key_index)) % (len(vigenere_alphabet)))]

            ## Rotate Key Forward ##
            key_index =+ 1
            if key_index == len(key):
                key_index = 0
                
        elif c in alphabet:
            decrypted_message += alphabet[((alphabet.index(c) + key.index(key_index)) % (len(alphabet)))]

            ## Rotate forward in Key##
            key_index += 1
            if key_index == len(key):
                key_index = 0

        else:
            decrypted_message += c

    return decrypted_message

Learned how to use the debugger and if I’m reading this right, the problem is that key_index is an integer but needs to be a string.

However, I’m not sure how to resolve that issue as I need that number to index the key to the proper location.

1 Like

It sounds like you may need to trace what’s going on in your code. Indeed, an index should rarely be used as a string, but that simply implies that at some point you accidentally asked some index to be a string where you probably meant the value at that index to be the string. (This sometimes happens when iterating).

By trace, I mean adding formatted prints that let you know know what each variable is at each point of change.

1 Like

That is where I’m getting thrown off and I think my error may just be right in front of me and I’m not thinking it through. The idea is that alphabet.index© is taking the value of alphabets index at location of c and adding it to the value of key’s index at the current index of key_index.

I’m thinking since key_index is going to be an integer always and c is going to be the current character, or a string that may be causing the issue.

However, I’m not sure how I can apply the same logic of © to key index in another manner. without getting it to rotate each time.

As soon as I typed the above response out I realized what was going on, I modified the code as to below

I was using the wrong idea. Now we are using the same alphabet and index, but indexing it to the character in key at the index position as rotated through the key.


def vigenere_decrypt(message, key):
    decrypted_message = ""
    key_index = 0
    for c in message:
        if c in vigenere_alphabet:
            # # This needs to find the index of key_index at alphabet, value for index of this one.  Original Index +
            # index of key ##
            decrypted_message += vigenere_alphabet[((vigenere_alphabet.index(c) + vigenere_alphabet.index(key[key_index])) % (len(vigenere_alphabet)))]

            ## Rotate Key Forward ##
            key_index += 1
            if key_index == len(key):
                key_index = 0

        elif c in alphabet:
            decrypted_message += alphabet[((alphabet.index(c) + alphabet.index(key[key_index])) % (len(alphabet)))]

            ## Rotate forward in Key##
            key_index += 1
            if key_index == len(key):
                key_index = 0

        else:
            decrypted_message += c

    return decrypted_message

While the code runs without errors and keeps spacing just right, it is not quite giving me the correct result, which is because I was doing it wrong again. I changed the index value from + to -. Message decrypts perfectly.

1 Like