I'm following a course, where you write a caesar cipher. After doing the task myself with slightly different code (which worked), I have now tried to replicate the code exactly from the course, and it does not seem to work (ChatGPT says it should be working too)
If I choose to 'decode', type the letter 'C' and shift it by 2 (which will become -2), it correctly shows the letter 'a'.
But if i try to decode a full word, for example by choosing 'decode', writing 'khoor' and shifting by '3', it should show 'hello', but instead shows 'hklro'.
Here is the code:
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
def caesar(encode_or_decode,original_text, shift_amount):
output_text = ""
for letter in original_text:
if encode_or_decode == "decode":
shift_amount *= -1
shifted_position = alphabet.index(letter) + shift_amount
shifted_position %= len(alphabet)
output_text += alphabet[shifted_position]
print(f"Here is the {encode_or_decode}d result: {output_text}")
caesar(original_text=text, shift_amount=shift, encode_or_decode=direction)
My input was
"decode", "khoor", "3"
You test encode_or_decode
inside the for
loop, and if its decode
, you keep multiplying the shift_amount
by -1
on each iteration, which causes the wrong result when decoding. Move the check outside the loop, and you should be fine:
def caesar(encode_or_decode,original_text, shift_amount):
output_text = ""
if encode_or_decode == "decode": # encode_or_decode check moved here
shift_amount *= -1
for letter in original_text:
shifted_position = alphabet.index(letter) + shift_amount
shifted_position %= len(alphabet)
output_text += alphabet[shifted_position]
print(f"Here is the {encode_or_decode}d result: {output_text}")