-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
63 lines (43 loc) · 1.8 KB
/
main.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
from morse_code_data import morse_code_map
def generate_morse(input_text):
input_text = input_text.strip()
res = ""
for char in input_text:
if char == " ":
res += " /"
else:
res += " " + morse_code_map[char.upper()]
return f"Morse Code: {res.strip()}"
def interpret_morse(input_morse):
input_morse = input_morse.strip()
morse_keys = list(morse_code_map.keys())
morse_values = list(morse_code_map.values())
res = []
for word in input_morse.split(" / "):
decoded_word = ""
for char in word.split(" "):
decoded_word += morse_keys[morse_values.index(char)]
res.append(decoded_word)
return f"Code Text: {' '.join(res)}"
welcome = """
╦ ╦┌─┐┬ ┌─┐┌─┐┌┬┐┌─┐ ┌┬┐┌─┐ ╔╦╗┌─┐┬─┐┌─┐┌─┐ ╔═╗┌─┐┌┬┐┌─┐
║║║├┤ │ │ │ ││││├┤ │ │ │ ║║║│ │├┬┘└─┐├┤ ║ │ │ ││├┤
╚╩╝└─┘┴─┘└─┘└─┘┴ ┴└─┘ ┴ └─┘ ╩ ╩└─┘┴└─└─┘└─┘ ╚═╝└─┘─┴┘└─┘
"""
while True:
print(welcome)
choice = input("Press 1 to generate Morse Code.\nPress 2 to Interpret a Morse Code.\n:")
try:
if choice == "1":
text = input("Input the text: ")
print(generate_morse(text))
elif choice == "2":
morse = input("Input the morse code: ")
print(interpret_morse(morse))
else:
print("Wrong Choice!")
except Exception as e:
print(f"An Error Occurred: {e}")
again = input("Press Y to run programme again.\nPress any other key to quit.\n:")
if again.upper() != "Y":
break