-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCode.py
More file actions
24 lines (19 loc) · 993 Bytes
/
Copy pathMorseCode.py
File metadata and controls
24 lines (19 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
morse_dict = {
'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': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.'
}
def encode_to_morse(text):
# Write code here
encoded_words = []
text = text.upper().strip()
for word in text.split():
encoded_letters = []
for char in word:
if char in morse_dict: # Only encode known characters
encoded_letters.append(morse_dict[char])
encoded_words.append(" ".join(encoded_letters))
return " / ".join(encoded_words)
txt = input("Give me your text:")
print(encode_to_morse(txt))