Skip to content

Instantly share code, notes, and snippets.

@DNDK
Created August 28, 2020 16:12
Show Gist options
  • Save DNDK/3aaf1ffc9614771e2fdeda6e965c029b to your computer and use it in GitHub Desktop.
Save DNDK/3aaf1ffc9614771e2fdeda6e965c029b to your computer and use it in GitHub Desktop.
Caesar encoding
alph = [
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmopqrstuvwxyz",
]
text = input("Enter the text\n>>")
shift = input("Enter the shift\n>>")
try:
shift = int(shift)
except ValueError:
print("Shift must be a number!")
else:
encoded = ""
for i in text:
if i.isalpha():
if i.isupper():
index = alph[0].index(i)
index +=shift
while index>26:
index -= 26
encoded += alph[0][index]
else:
index = alph[1].index(i)
index += shift
while index>26:
index -= 26
encoded += alph[1][index]
else:
encoded+=i
print(encoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment