-
Notifications
You must be signed in to change notification settings - Fork 0
/
39_Secret_code_language.py
52 lines (40 loc) · 1.42 KB
/
39_Secret_code_language.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
from random import choice
def random_string(len):
""" Get randome text by the given length """
characters = "abcdefghijklmnopqrstuvwxyz"
return ''.join(choice(characters) for i in range(len))
def encode_string(string):
"""
Encode the given string in the defined format
"""
if len(string) < 3:
# Reverse the string if the length of the string is less than 3
print(f"Your encrypted word is: {string[::-1]}")
return string[::-1]
else:
# Encrypt the text
encoded_string = random_string(3) + string[1:] + string[:1] + random_string(3)
print(f"Your encrypted string is: {encoded_string}")
return encode_string
def decode_string(string):
"""
Decode the given string in the defined format
"""
if len(string) < 3:
print(f"Your decrypted text is: {string[::-1]}")
return string[::-1]
else:
# Decrypt the text
decode_string = string[3:-3]
decoded_string = decode_string[len(decode_string) - 1] + decode_string[:-1]
print(f"Your decrypted text is: {decoded_string}")
return decoded_string
code = input("Enter your text: ")
print("\nWhat you want to do with the text? \n 1: Encryption\n 2: Decryption \n")
action = int(input("Select your method: "))
if action == 1:
encode_string(code)
elif action == 2:
decode_string(code)
else:
print("Please select a valid method for action")