-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathCaesar_cipher_python
31 lines (26 loc) · 1.31 KB
/
Caesar_cipher_python
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
"""Caesar Cipher
The Caesar cipher is a type of substitution cipher in which each letter in the plaintext is shifted to a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is named after Julius Caesar, who allegedly used it to communicate with his generals.
For example:
Input: "defend the east wall of the castle"
Output: "efgfoe uif fbtu xbmm pg uif dbtumf"
Write a program that encrypts the user input using the Caesar cipher. *bonus: write the decryption cipher."""
caesar_var, phrase = input("a shift number for the Caesar cipher, followed by '/' and then your phrase").split('/')
def caesar(phrase):
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']
print ('\n')
phrase = phrase.lower()
break_at_space = phrase.split(" ")
new_phrase = ' '
for word in break_at_space:
new_word = [ ]
for letter in word:
if letter in alphabet:
spot = alphabet.index(letter)
new_spot = spot + int(caesar_var)
new_letter = alphabet[new_spot]
new_word += new_letter
FINAL = str.join('', (new_word))
new_phrase += (" " + FINAL)
new_phrase = str.join('', (new_phrase))
return (new_phrase)
print ("Your new phrase is:" + caesar(phrase))