# Off Platform - Coded Correspondence - Not understanding

This is my first post on here and I am needing help understanding the Off Platform - Coded Correspondence project

I had to rely heavily on to solutions part of it to complete the project. After reading through the solutions for the first 4 question I was able to see the the for loop was going through each letter for the message finding the numeric value of that letter and adding 10 to find the correct letter.

But for questions 5 and 6 I understand it is the same concept but I was not understanding how you iterate across the keyword to get the values that you needed to solve the message. Could someone help explain what was happening in the code below ?

Thank you for your help !

For step 6, I solved it as follows. You can read my comments to understand how I solved it. I’ve customized the message and keyword at the top of the page, but you can enter anything and it should work.

``````alphabet = 'abcdefghijklmnopqrstuvwxyz'
message = "My favourite morning fruit are berries"
keyword_base = "Yoghurt"

# Create a function that nests the keyword_generator and encryptor functions.
def power_encryptor (message, keyword_base):

# Function to generate keyword
def keyword_generator (encrypted_message, keyword_base):
keyword = ''
i = 0
for char in encrypted_message:
# Check if character isn't punctuation, in which case we don't want to adjust it.
if char in alphabet:
# Gives the char in keyword_base in an iterative index (e.g. 0, 1, 2, etc) capped at the index of length of keyword_base.
keyword += keyword_base[i % len(keyword_base)]
i += 1
else:
keyword += char
return keyword

# Function to encrypt
def encryptor (message, keyword):
encrypted_message = ''
i = 0
for char in message:
# Check for punctuation, which is copied without any adjustments.
if char in alphabet:
# Calculate the index based on the offset of the char in the encrypted_message vs. keyword. To decrypt, we don't decrease the index based on the offset, but instead increase it.
# I had to change the chars to lowercase because they wouldn't match in the alphabet
alphabet_index = len(alphabet) + alphabet.find(char.lower()) - alphabet.find(keyword[i].lower())
# Add the decrypted letter to the decrypted_message. Cap by the length of the alphabet.
encrypted_message += alphabet[alphabet_index % len(alphabet)]
i += 1
else:
encrypted_message += char
i += 1
return encrypted_message
# Return both the encrypted message and keyword
return encryptor (message, keyword), keyword_generator (encrypted_message, keyword_base)

# Define keyword globally.
keyword = power_encryptor(message, keyword_base)[1]
print('keyword:', keyword)

# Define encrypted_message globally.
encrypted_message = power_encryptor(message, keyword_base)[0]
print('Encrypted message:', encrypted_message)

# Decrypt the encrypted message with the custom keyword
def decryptor (encrypted_message, keyword):
decrypted_message = ''
i = 0
for char in encrypted_message:
# Check for punctuation, which is copied without any adjustments.
if char in alphabet:
# Calculate the index based on the offset of the char in the encrypted_message vs. keyword. To decrypt, we don't decrease the index based on the offset, but instead increase it.
# I had to change the chars to lowercase because they wouldn't match in the alphabet
alphabet_index = len(alphabet) + alphabet.find(char.lower()) + alphabet.find(keyword[i].lower())
# Add the decrypted letter to the decrypted_message. Cap by the length of the alphabet.
decrypted_message += alphabet[alphabet_index % len(alphabet)]
i += 1
else: