-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar_Cipher.py
31 lines (24 loc) · 918 Bytes
/
Caesar_Cipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
FIRST_CHAR_CODE = ord("A")
LAST_CHAR_CODE = ord("Z")
CHAR_RANGE = LAST_CHAR_CODE - FIRST_CHAR_CODE + 1 #coz index count starts from 0
def caesar_shift(message, shift):
#result placeholder
result = ""
# go through each of the letters in the message.
for char in message.upper():
if char.isalpha():
#convert charc into ASCII code
char_code = ord(char)
new_char_code = char_code + shift
if new_char_code > LAST_CHAR_CODE:
new_char_code -= CHAR_RANGE
if new_char_code < FIRST_CHAR_CODE:
new_char_code += CHAR_RANGE
new_char = chr(new_char_code)
result = result + new_char
else:
result = result + char
print(result)
user_message = input("Message to Encrypt: ")
user_shift_key = int(input("Shift Key (int): "))
caesar_shift(user_message, user_shift_key)