# Coded Correspondence: decoding the Vigenère

Hi. Having trouble with the last task in the Coded Correspondence project, my function’s output is weird and I can’t for the life of me figure out why.

The point is to decipher the string (“message” variable) encoded with the Vigenère’s Code. The key is in “key” variable.

I know it’s not optimal but I do the cycle twice: one to generate the keyphrase (which appears to be done correctly) and another to decipher the message with the keyphrase. It seems to be doing alright for the first word (unless it’s a coincidence somehow) but goes bonkers after that. I don’t get it, maybe I misunderstand the principle? The difference bettween the letters in the initial and in the target message should be equal to, let’s say it this way, the number of the corresponding letter of the keyphrase, no?

``````alphabet = "abcdefghijklmnopqrstuvwxyz"
total_letters = len(alphabet)
message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
key = "friends"

def decode_vig(message, key):
decoded = ""
keyphrase = ""
keyphrase_counter = 0
key_length = len(key)
for letter in message:
if letter in alphabet:
keyphrase += key[keyphrase_counter]
keyphrase_counter = (keyphrase_counter + 1) % key_length
else:
keyphrase += letter

current_position = 0
for letter in message:
if letter in alphabet:
letter_index = alphabet.find(letter)
shift = alphabet.find(keyphrase[current_position])
#print(shift)
decoded += alphabet[(letter_index - shift) % total_letters]
current_position += 1
else:
decoded += letter

print(message)
print(keyphrase)

return decoded

print(decode_vig(message, key))
``````

The output is (the message, the keyphrase, the decoded message):

dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!
fri ends frie nd sfrien dsfr? iend sfri! end sfr iendsfri endsf rie ndsfri en dsfriendsfr!
you bnht ntor yn wwqxpm frjw? wjxc xujk! you teq saqsaabp ccumb gcf zvqbjx rd neetogfqeuk!

Beneath is the copy pasted code I put into visual with the steps I used to figure out the problem.
Beneath that is a hint and a blurred out answer within the hint

happy coding

Summary
``````alphabet = "abcdefghijklmnopqrstuvwxyz"
total_letters = len(alphabet)
message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
key = "friends"

def decode_vig(message, key):
decoded = ""
keyphrase = ""
keyphrase_counter = 0
key_length = len(key)
for letter in message:
if letter in alphabet:
keyphrase += key[keyphrase_counter]
keyphrase_counter = (keyphrase_counter + 1) % key_length
else:
keyphrase += letter

current_position = 0
for letter in message:
print(letter)
if letter in alphabet:
#started here printing the letter
letter_index = alphabet.find(letter)
print(alphabet.find(letter))
shift = alphabet.find(keyphrase[current_position])
#print(shift)    #moved here to see how the shift was handled
decoded += alphabet[(letter_index - shift) % total_letters]
print(decoded)  #ended here by inspecting the terminal output verse provided code
current_position += 1
else:
decoded += letter

print(message)
print(keyphrase)

return decoded

decode_vig(message, key)
``````

hint and blurred solution:

Summary

a hint before you click is to look at letter positioning as it progresses during cyphering.

else:
decoded += letter
current_position += 1