Code Review Request > Coded Correspondence > Vigenere Cipher Exercise

Hi Everyone,
I’m 41% through the Data and Programming Foundations for AI course. I’m struggling with the Vigenere Cipher exercise in Coded Correspondence. I’ve needed a lot of help from Grok and ChatGPT but even with that although the code runs, it outputs gobbledegook.

Any thoughts appreciated!

Thanks
Simon

here’s the gobbledegook output:

“you fugr doqd lc pmoyei ucgt? nice jaig! mgi mzq yrxphgod ulser the umcqip zy qhxpjujofgu!”

Here’s the code:

Setup

coded_message = “txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!”
keyword = “friends”
alphabet = “abcdefghijklmnopqrstuvwxyz”

Step 1: Generate keyword_phrase

keyword_phrase = ‘’
for i in range(len(coded_message)):
keyword_phrase += keyword[i % len(keyword)]

Step 2: Get offsets

offsets =
for i in range(len(coded_message)):
char = coded_message[i]
key_char = keyword_phrase[i]
if char in alphabet:
offsets.append(alphabet.index(key_char))
else:
offsets.append(" ")

Step 3: Decode (add offsets)

decoded = ‘’
for i in range(len(coded_message)):
char = coded_message[i]
if char in alphabet:
char_pos = alphabet.index(char)
offset = offsets[i]
new_pos = (char_pos + offset) % 26 # Add for this cipher
decoded += alphabet[new_pos]
else:
decoded += char

Result: offsets is the list of offsets, decoded is the plaintext

For checking, you can print:

print(offsets) # [1, 4, 5, None, …]

print(decoded) # The decoded message

Using CGPT or the like are not a good way to learn, use critical thinking skills, or retain information when learning to code.
Seriously, you’re way better off not using either and rather looking at: python documentation & searching the forums here for this specific project b/c others have posted about it and the issues they faced/are facing.

Also, it’s difficult to read unformatted python code. If someone is going to help you, they’re going to copy your code and then they have to format it to help you debug it.
Please format your code. Use the “</>” button above and see: