Stuck at coded correspondence task 4


To complete the 4th task in the coded corrspondence assignment I’ve written the following code to decipher a message:

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = "!.,? "

def decoder(message, offset):
    translated_message = ""
    for character in message: 
        if not character in punctuation:
            index_character = alphabet.find(letter)
            translated_message += alphabet[(index_character + offset) % 26]
            translated_message += character
    return translated_message

coded_message = "jxu evviuj veh jxu iusedt cuiiqwu yi vekhjuud." 
print(decoder(coded_message, 10))

However, when I run this code it seems to convert every character in the coded message to the letter “j”. I cannot quite figure out what I’m doing wrong here. Any suggestions would be much appreciated! :slight_smile:

Best wishes, Alexander.


index_character = alphabet.find(letter)

letter ?

I assume you’ve defined it earlier in your notebook for something else, which is why it’s not throwing up an error.

Hope that helps.

Thanks! I changed ‘letter’ to ‘character’ and it works just fine. Can’not believe I missed that! :grimacing:

Best wishes, Alexander.

1 Like