Off-Platform Project: Coded Correspondence - String index out of range

I’m having trouble running the Vigenère Cipher program I wrote for the Coded Correspondence part two assignment on Jupyter. The code I used was the same as the one provided by Cody Academy, but I’m still getting an error message and can’t get it to work.


import string def vigenere_decode(message, keyword): keyword_phrase = "" keyword_index = 0 #use to iterate through keyword alphabet = ''.join(chr(letter) for letter in range(97, 123)) # ASCII values for a-z are 97-122 punctuation = string.punctuation #print punctuation for letter in message: if letter not in punctuation: keyword_phrase += keyword[(keyword_index) % len(keyword)] keyword_index += 1 #increment else: keyword_phrase += letter decoded_message = "" keyword_index = 1 #use to iterate through keyword_phrase for letter in message: if letter not in punctuation: decoded_message += alphabet[(alphabet.find(letter) - alphabet.find(keyword_phrase[keyword_index])) % 26] else: decoded_message += letter keyword_index += 1 #increment outside of if - else stuff return decoded_message vigenere_message = "txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!" vigenere_keyword = "friends" print(vigenere_decode(vigenere_message, vigenere_keyword))

check the indentation of the else in the first for-loop.

punctuation is missing a space character,
so you might want to do
punctuation += ' '
immediately after you create that variable.

You had
keyword_index = 1
to set up the keyword_index variable,
but the first possible index would be 0, not 1.

You may want to change the minus to be a plus [where you’re doing the math for the decoding].

1 Like

thank you! that helps!