-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cypher.py
148 lines (129 loc) · 5.43 KB
/
caesar_cypher.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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 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'
# alpha = alphabet.split()
# end_of_game = False
#
# while end_of_game:
# what_to_do = input("Type 'encode' to encrypt and 'decode' to decrypt\n:")
# message = input("Enter your message here\n:").lower()
# shift_num = int(input("Enter your shift number\n:"))
#
# go_again = input("Want to go again? Yes or No \n").lower()
# if go_again == 'no':
# end_of_game = True
# print("Goodbye, cone back again soon!")
# else:
# end_of_game = False
#
# new_msg = []
#
#
# def encrypt(msg, shift):
# shift %= 25
# encrypted_msg = ''
# for char in msg:
# if char in alpha:
# char_position = alpha.index(char)
# char = alpha[char_position + shift]
# new_msg.append(char)
# encrypted_msg = ''.join(new_msg)
# else:
# new_msg.append(char)
# print(f'Your encrypted message is {encrypted_msg}.')
#
#
# encrypt(msg=message, shift=shift_num)
# 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 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'
# alpha = alphabet.split()
# end_of_game = False
#
# while not end_of_game: # Changed from "end_of_game" to "not end_of_game"
# what_to_do = input("Type 'encode' to encrypt and 'decode' to decrypt:\n") # Removed unnecessary ":" from input
# # prompt
# message = input("Enter your message here:\n").lower()
# shift_num = int(input("Enter your shift number:\n"))
#
# go_again = input("Want to go again? Yes or No:\n").lower()
# if go_again == 'no':
# end_of_game = True
# print("Goodbye, come back again soon!")
# else:
# end_of_game = False
#
#
# def encrypt(msg, shift):
# shift %= 26 # Changed from "25" to "26"
# encrypted_msg = ''
# for char in msg:
# if char in alpha:
# char_position = alpha.index(char)
# char = alpha[char_position + shift]
# encrypted_msg += char # Concatenate to encrypted_msg
# else:
# encrypted_msg += char # Concatenate to encrypted_msg
# print(f'Your encrypted message is {encrypted_msg}.') # Moved outside the loop
#
#
# encrypt(msg=message, shift=shift_num) # Moved outside the else block
# def encrypt(msg, shift):
# encrypted_msg = ''
# for char in msg:
# if char in alpha:
# char_position = alpha.index(char)
# char = alpha[char_position + shift]
# encrypted_msg += char
# else:
# encrypted_msg += char
# print(f'Your encrypted message is: {encrypted_msg}')
# encrypt(msg=message, shift=shift_num)
# def decrypt(msg, shift):
# decrypted_msg = ''
# for char in msg:
# if char in alpha:
# char_position = alpha.index(char)
# char = alpha[char_position - shift]
# decrypted_msg += char
# else:
# decrypted_msg += char
# print(f'Your decrypted message is: {decrypted_msg}')
# decrypt(msg=message, shift=shift_num)
# if what_to_do == 'encode':
# encrypt(msg=message, shift=shift_num)
# elif what_to_do == 'decode':
# decrypt(msg=message, shift=shift_num)
# else:
# print('We don\'t understand your command. Enter "encode" to encrypt and "decode" to decrypt.\n')
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 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'
alpha = alphabet.split()
not_end_of_game = True
def caesar(command, msg, shift):
end_text = ''
if command == 'decode':
shift *= -1
for char in msg:
if char in alpha:
char_position = alpha.index(char)
char = alpha[char_position + shift]
end_text += char
else:
end_text += char
print(f'Your {command}d message is: {end_text}')
while not_end_of_game:
print('''
_______ _______ _______ _______ _______ _______ _______ _______ _______ _______
( ____ \( ___ )( ____ \( ____ \( ___ )( ____ ) ( ____ \|\ /|( ____ )|\ /|( ____ \( ____ )
| ( \/| ( ) || ( \/| ( \/| ( ) || ( )| | ( \/( \ / )| ( )|| ) ( || ( \/| ( )|
| | | (___) || (__ | (_____ | (___) || (____)| | | \ (_) / | (____)|| (___) || (__ | (____)|
| | | ___ || __) (_____ )| ___ || __) | | \ / | _____)| ___ || __) | __)
| | | ( ) || ( ) || ( ) || (\ ( | | ) ( | ( | ( ) || ( | (\ (
| (____/\| ) ( || (____/\/\____) || ) ( || ) \ \__ | (____/\ | | | ) | ) ( || (____/\| ) \ \__
(_______/|/ \|(_______/\_______)|/ \||/ \__/ (_______/ \_/ |/ |/ \|(_______/|/ \__/
''')
what_to_do = input("Type 'encode' to encrypt and 'decode' to decrypt:\n")
message = input("Enter your message here:\n").lower()
shift_num = int(input("Enter your shift number:\n"))
shift_num %= 26
caesar(command=what_to_do, msg=message, shift=shift_num)
go_again = input("Want to go again? Yes or No:\n").lower()
if go_again == 'no':
not_end_of_game = False
print("Goodbye, come back again soon!")