-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-to-morse.py
58 lines (49 loc) · 1.92 KB
/
text-to-morse.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
import os
from art import logo
# first the letters (a-z), then the numbers (0-9)
morse_letters = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."]
WHITE = '\033[0m' # white
RED = '\033[31m' # red
GREEN = '\033[32m' # green
ORANGE = '\033[33m' # orange
BLUE = '\033[34m' # blue
PURPLE = '\033[35m' # purple
def clear():
os.system('clear')
def is_valid(message):
validated = True
for char in message:
ascii_code = ord(char)
if char.isalnum() or (ascii_code == 32):
pass
else:
print(f"{RED}\nERROR: char '{char}' (Ascii code {ord(char)}) not allowed.")
validated = False
break
return validated
def morse(message):
if is_valid(message):
print(f"\n{ORANGE}Morse code")
for char in message:
ascii_code = ord(char)
if (ascii_code == 32):
print(" ") # Used to force one word per row
else:
# ternary operator
offset = 97 if ascii_code >= 97 else 48-26 # offset for 'a' or '0' in the list (0 is after the 26 letters)
index = ord(char)-offset
print(f"{GREEN}{morse_letters[index]}", end=" ")
print("\n")
else:
print(f"{ORANGE}The message should contain only letters and numbers.\n")
should_end = False
while not should_end:
clear()
print(BLUE+logo)
message = input(f"\n{WHITE}Type your message: ").lower()
morse(message)
restart = input(f"{WHITE}Type 'yes' to encode anothe message. Otherways type 'no': ")
if restart == "no":
should_end = True
print(f"\n{GREEN}Goodbye\n")