Skip to content

Instantly share code, notes, and snippets.

@gauravpatt92
Created June 2, 2017 10:44
Show Gist options
  • Save gauravpatt92/bcb65c06fce1e5935c2b39c212824fd9 to your computer and use it in GitHub Desktop.
Save gauravpatt92/bcb65c06fce1e5935c2b39c212824fd9 to your computer and use it in GitHub Desktop.
def caesar_encryption(st, shift, num):
new = st.lower()
result = ""
if shift=='l':
for i in new:
if i.isalpha():
if ord(i)-num<97:
result = result + chr(122-(97-(ord(i)-num) +1))
else:
result = result + chr(ord(i)-num)
else:
result = result + i
elif shift=='r':
for i in new:
if i.isalpha():
if ord(i)+num>122:
result = result + chr(97 + ((ord(i)+num)-122 -1))
else:
result = result + chr(ord(i)+num)
else:
result = result + i
return result
st = input("Enter the message to be encrypted: ")
shift = input("Enter 'l' or 'r' to mention direction of shift: " )
num = int(input("Enter number of shifts: "))
print("The encrypted message is : ",caesar_encryption(st, shift, num))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment