Coded_correspondence: Step 5 - Seeking Help with Error

Hello! I am getting an error and am searching for help on how to improve my code for the following problem:

Step 5: Learn Python 3 - Strings, Off-Platform Project: Coded Correspondence ](https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication)

I understand a solution is available, however I would like to know what I am doing wrong! Any help is appreciated, Thank you!

Code:


coded_message = 'dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!'
keyword_given = 'friends'

def decoder(message, keyword):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    message_decoded = ''
    keyword_phrase = ''
    for i in range(len(message)):
        if message[i] in alphabet:
            keyword_phrase += keyword[i % len(keyword)]
            keyword_phrase_value = alphabet.find(keyword_phrase[i] % 26)
            coded_message_value = alphabet.find(message[i] % 26)
            message_decoded += alphabet[keyword_phrase_value + coded_message_value % 26]
        else:
            message_decoded += message[i]
    print(message_decoded)
decoder(coded_message, keyword_given)    

TypeError                                 Traceback (most recent call last)
<ipython-input-193-b33e9a942151> in <module>
     15             message_decoded += message[i]
     16     print(message_decoded)
---> 17 decoder(coded_message, keyword_given)

<ipython-input-193-b33e9a942151> in decoder(message, keyword)
      9         if message[i] in alphabet:
     10             keyword_phrase += keyword[i % len(keyword)]
---> 11             keyword_phrase_value = alphabet.find(keyword_phrase[i] % 26)
     12             coded_message_value = alphabet.find(message[i] % 26)
     13             message_decoded += alphabet[keyword_phrase_value + coded_message_value % 26]

TypeError: not all arguments converted during string formatting

Hey there @netplayer13148!! Welcome to the Codecademy forums :grinning:

The first thing I notice in your code is the way your string slices are written, take line 11 for instance:

keyword_phrase[i] % 26

What are you using the % on?
i, in this instance, can only be operated on from inside the square brackets, however currently it is being used on a slice from keyword_phrase.

1 Like

Thank you @8-bit-gaming. With your help I was able to solve the problem!!

For anyone interested here is my final solution!! :slight_smile:



coded_message = 'dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!'
keyword_given = 'friends'

def decoder(message, keyword):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    message_decoded = ''
    keyword_phrase = ''
    keyword_placeholder = 'a'
    counter = 0
    for i in range(len(message)):
        if message[i] in alphabet:
            keyword_phrase += keyword[(i - counter) % len(keyword)]
        else:
            keyword_phrase += keyword_placeholder
            counter += 1 
    for i in range(len(message)):
        if message[i] in alphabet:
            keyword_phrase_value = alphabet.find(keyword_phrase[i])
            coded_message_value = alphabet.find(message[i])
            message_decoded += alphabet[coded_message_value - keyword_phrase_value]
        else:
            message_decoded += message[i]
            coded_message_value = 0
    return(message_decoded)
print(decoder(coded_message, keyword_given))                           


1 Like