-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
32 lines (26 loc) · 1.04 KB
/
caesar.py
File metadata and controls
32 lines (26 loc) · 1.04 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
import string
def alphabet_position(letter):
"""Receives a character, letter, and returns the 0-based
numerical position of that letter in the alphabet."""
lowerletter = letter.lower()
index = string.ascii_lowercase.find(lowerletter)
return index
def rotate_character(char, rot):
"""Receives a character, char, and an integer, rot, and returns string
newchar the character rotated rot alphabetic places to the right."""
if not char.isalpha():
return char
newindex = (alphabet_position(char) + rot) % 26
if char.isupper():
newchar = string.ascii_uppercase[newindex]
else:
newchar = string.ascii_lowercase[newindex]
return newchar
def encrypt(text, rot):
"""Receives a string, text, and an integer, rot, and returns an encrypted
string, encrypt_str, that is comprised of the characters of text rotated
rot places to the right alphabetically."""
encrypt_str = ""
for char in text:
encrypt_str = encrypt_str + rotate_character(char, rot)
return encrypt_str