alphabet = "abcdefghijklmnopqrstuvwxyz" m1 = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!" def decode_mesage(message): m0 = "" for i in range(len(message)): letter_loc = alphabet.find(message[i]) if letter_loc == -1: m0 += message[i] if letter_loc < 16: m0 += alphabet[letter_loc + 10] if letter_loc >= 16: m0 += alphabet[letter_loc + 10 - 26] return m0 print(decode_mesage(m1))
hey jthere!j jthis jis jan jexample jof ja jcaesar jcipher.j jwere jyou jable jto jdecode jit?j ji jhope jso!j jsend jme ja jmessage jback jwith jthe jsame joffset!j

output is weird with extra j after each symbol, any idea what caused this?

I see the cause here:
if letter_loc < 16:
if the letter is not in alphabet, then alphabet.find( ) gives you -1
so letter_loc would be -1
and -1 < 16 is True
so m0 += alphabet[letter_loc + 10] runs
and alphabet[-1 + 10] happens to be j

You can fix that by changing the second and third if to be elif

Alternatively, you could change
if letter_loc < 16:
to be
if letter_loc < 16 and letter_loc != -1:

Thank you so much for the explanation. You are right, I should be careful about writing conditions next time and make sure they are independent. I guess using ‘elif’ is a good way avoiding this type of error. Really appreciate your time for checking up on my code.