-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
32 lines (24 loc) · 1005 Bytes
/
caesar.py
File metadata and controls
32 lines (24 loc) · 1005 Bytes
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
32
alphabet = "abcdefghijklmnopqrstuvwxyz"
message = input("What is your message? English alphabets only.\n> ")
num = int(input("\nBy how many spaces to the right? Integers only.\n> "))
def code(message, num):
coded = ""
lowercase = message.lower()
for i in range(0,len(message)):
if message[i] == " ": # check if it's a space
decoded_letter = " "
else:
new_index = alphabet.find(lowercase[i]) + num
if new_index>26: # check if the new index is out of range of alphabet
while new_index>26:
new_index -= 26
elif new_index<0:
while new_index<0:
new_index += 26
decoded_letter = alphabet[new_index]
if message[i].isupper() == True: # make originally uppercase characters uppercase
decoded_letter = decoded_letter.upper()
coded += decoded_letter
i += 1
print(f"\nOutput is:\n{coded}")
code(message, num)