I'm currently making a caesar function and in the process of optimization, ran across a really weird problem when decrypting an encoded text. For example, encoding civilization with a shift value = 5 results in the output "hnanqnefynts", however upon decoding it results in "csvslszktsox", notice that the odd positions in the word decode correctly, while the even positions are instead shifted by 10, e.g: i -> s, a -> k, n -> x.
I believe the main bug from the code comes from using an if statement to make the shift value negative instead of a minus operator and how it works with the modulo operator, although I don't quite understand why only the even positions in the resulting word is affected.
def caesar(plain_text, shift_amount, cipher_direction):
text_to_list = list(text)
output = ''
operation = 'encoded'
for letter in text_to_list:
letter_int = alphabet.index(letter)
if cipher_direction == 'decode':
shift_amount *= -1
operation = 'decoded'
processed_letter = alphabet[(letter_int + shift_amount) % len(alphabet)]
output += processed_letter