Vigenere Project

Good evening,

I’m working on solving the Vigenere decoding project and honestly am struggling. Below, I have the code that I’ve come up with so far, but it doesn’t seem to solve the cipher. Any insight on what is wrong with my code? Thank you!

import string alphabet = string.ascii_lowercase def vigenere_cipher(coded_message, keyword): solved_cipher = '' key_index = 0 for i in range(len(coded_message)): if coded_message[i] in alphabet: solved_cipher += alphabet[(alphabet.index(coded_message[i]) + alphabet.index(keyword[key_index % len(keyword)])) % len(alphabet)] key_index += 1 else: solved_cipher += coded_message[i] return solved_cipher print(vigenere_cipher('txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!', 'friends'))

Are we missing an alphabet object?

Sorry, no I forgot to include it when pasting the code but I do have it defined in my original code - I just added it.

1 Like

Please repost your code with the missing alphabet object.

1 Like

I just did! Thank you!

1 Like

If you can still edit, please add a print line that calls the function.

1 Like

Done - thanks for walking me through it - it’s my first time posting using the codebyte so it’s a learning process

1 Like

Sorry I actually realized I was decoding the wrong message with the wrong key (yikes, it’s been a long day). But of note, I’m having trouble figuring out how to space out the individual words. I thought the else: solved_cipher += coded_message[i] would do it but there’s no spacing between each word. I’ve updated the code as well with the correct message.

1 Like

Have tried adding a space character to your alphabet?

How exactly would I do that? When I try to add ’ ’ to alphabet, it throws off the cipher

It may need some added logic. If you are able to pass this exercise, now would be a good time to bookmark it, move on and come back in a week or two. Give your brain a chance to take in some new material while it cognates what you’ve inputted at this point. Take the small success and return with a new vision.

Okay, thank you! It definitely is frustrating when it feels like just a small piece of the puzzle is missing, but I also understand that sometimes a different time/place will spark the “aha” moment I need. Thanks for all your help! Glad to know I’m learning something!

1 Like

check whether the else is indented correctly. It should be indented the same amount as the if

2 Likes

That did it! Thank you so much - it needed to be moved over an extra space