Skip to content

Commit 00635fc

Browse files
authored
Add files via upload
Caeser cipher algorithm was written using Python 3.
1 parent c8c9d29 commit 00635fc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

caeser_cipher_algorithm.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
2+
3+
def caeser(text, shift, direction):
4+
result_text = ""
5+
if direction == "decode":
6+
shift *= -1
7+
for char in text:
8+
if char in alphabet:
9+
position = alphabet.index(char)
10+
new_position = position + shift
11+
new_position = new_position % len(alphabet)
12+
result_text += alphabet[new_position]
13+
else:
14+
result_text += char
15+
return result_text
16+
17+
terminate_program = False
18+
while not terminate_program:
19+
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
20+
text = input("Type the message:\n").lower()
21+
shift = int(input("Type the shift:\n"))
22+
result = caeser(text, shift, direction)
23+
print(result)
24+
program_continue = input("If you want to continue type 'yes'. Otherwise type 'no'.\n").lower()
25+
if program_continue == "no":
26+
terminate_program = True
27+
print("The program was terminated.")

0 commit comments

Comments
 (0)