-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
46 lines (35 loc) · 1.34 KB
/
caesar.py
File metadata and controls
46 lines (35 loc) · 1.34 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def alphabet_position(letter, rotate):
# initialize the return message
new_rot = ""
# creates lower and upper alphabet variables
low_alpha = "abcdefghijklmnopqrstuvwxyz"
upper_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# checks to see if the character is upper or lower case and rotates
# the character by the rotation
if letter in low_alpha:
new_rot = rotate_character(low_alpha.index(letter), rotate)
new_rot = low_alpha[new_rot]
elif letter in upper_alpha:
new_rot = rotate_character(upper_alpha.index(letter), rotate)
new_rot = upper_alpha[new_rot]
# returns the final message
return new_rot
def rotate_character(char, rot):
newLetter = char + rot
# there are 26 letters in the alphabet and the index
# starts at zero so anything past 25 is returned to the
# remainder of the equation following the test condition
if newLetter >= 26:
newLetter = (newLetter % 26)
#print(char, rot, newLetter)
return newLetter
def encrypt(text, rot):
# initializes variables and takes the rot to lower case
message_encrypt = ""
# iterates through the loop through the entire message
for i in text:
if not i.isalpha():
message_encrypt += i
else:
message_encrypt += alphabet_position(i, rot)
return message_encrypt