-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCesear.py
51 lines (43 loc) · 1.6 KB
/
Cesear.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import string
def caesar_crypto_encode(text, shift):
output = ''
mover = ''
# returns empty if string is null or just space
if not text:
return ''
elif text.isspace():
return ''
# creates a dictorary with lowercase letters
letter_dict = dict(zip(string.ascii_lowercase, range(1, 27)))
# if the shift is greater than 26 the shift is just repeated this finds the shift
if shift >= 26 or shift <= -26:
shift = shift % 26
# if the shift is betwen 0 and -26 we add positive 26 to have it shift correclty
if shift > -26 and shift < 0:
shift = shift + 26
for i in text:
if i.isalpha():
if i.isupper():
iscap=True
elif i.islower():
iscap=False
i = i.lower()
for key, value in letter_dict.items():
if i == key:
mover = value + shift
# checks to see if the shift if out of range and if so we inverse it
if mover > 26 or mover < -26:
mover += (26 * (-shift / shift))
# Picks the letters out of the dictonary based on the shift
for key, value in letter_dict.items():
if mover == value:
if iscap:
output = output + key.upper()
elif not iscap:
output=output+key
else:
output = output + i
print(output)
#text = input("input text to be encoded ")
#shift = input("input amount to shift ")
#caesar_crypto_encode(text, int(shift))