Reverse code not working? - not all arguments converted during string formatting

I’m doing the “Coded Correspondence” cryptography project and getting stuck on the encoder although my decoder is working!

def v_decoder(message, keyword):
    decoded = ""
    l_counter = 0
    key_counter = 0
    for l in message:
        if l in alphabet:
            keyplace = alphabet.find(keyword[key_counter % len(keyword)])
            decoded += alphabet[(alphabet.find(l) - keyplace) % 26]
            l_counter += 1
            key_counter += 1
        else:
            decoded += l
            l_counter += 1
    return decoded
def v_encoder(message, keyword):
    encoded = ""
    l_counter = ""
    key_counter = ""
    for l in message:
        if l in alphabet:
            keyplace = alphabet.find(keyword[key_counter % len(keyword)])
            encoded += alphabet[(alphabet.find(l) + keyplace) % 26]
            l_counter += 1
            key_counter += 1
        else:
            encoded += l
            l_counter += l
    return encoded

on the keyplace = line I get the error:
TypeError: not all arguments converted during string formatting

I’m really not sure whats gone wrong because its just the reverse of the decoder. I’ve checked it in a text compare site!

It appears you are meaning to use % as a modulus operator, and it is being interpreted as an interpolation operator, because key_counter is of type string. The first example on this page demonstrates this type of misinterpretation pretty clearly:

# Since `x` is a string, the `%` operator is treated as a string
# interpolation operator. The error occurs because no format specifier
# is provided.
x = "11"
if(x % 2):
  print("x is odd")

Did you mean to set key_counter equal to an int, rather than to a string? It appears to be 0 in your v_decoder() function, and is incremented in both. If I correctly understand the purpose of it, setting it equal to 0 would solve your problem.

@825orion Thank you, I am a silly sausage.

Now I know what to look for in the future!

1 Like