READER BEWARE: The code in this post is horrible.
Ever been asked to write a Caesar Cipher in Python in 15 minutes? No? Neither have I.
Anyway, here is what I accomplished. It is far from optimal. It does not take a lot into account e.g. punctuation, uppercase chars, non integer keys, negative keys, etc. But I was in a hurry.
It takes the message variable and shifts each letter to the ‘right’ by the value of the current key in keys.
#!/usr/bin/env python3
from string import ascii_lowercase
# lowercase letters
CHARACTERS = list(ascii_lowercase)
# for char in CHARACTERS:
# print(ord(char))
message = "i cannot do this under duress"
keys = [1, 6, 9, 4, 2, 0]
# convert to unicode
message_ord = [ord(x) for x in list(message)]
for key in keys:
new_message = ""
for letter in message:
# I did take care of spaces.
if letter == " ":
new_message += " "
elif ord(letter) + key > 122:
#should prob mod something somewhere
offset = (ord(letter) + key) - 123
new_letter = 97 + offset
new_message += chr(new_letter)
else:
new_letter = ord(letter) + key
new_message += chr(new_letter)
print(f"For key: '{key}' the message is '{new_message}'")
This took me 15 minutes and 36 seconds.