-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText_and_MorseCode.py
70 lines (62 loc) · 2.29 KB
/
Text_and_MorseCode.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
morse_code = {
'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': '--..',
'1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----'
}
def text_to_morse_code():
task_type = input('What do you want to do? (encode/decode): ').lower()
if task_type == 'encode' or task_type == 'decode':
message = input('Enter you message here. Cannot use special characters: ').lower()
output = []
if task_type == 'encode':
for c in message:
if c in morse_code:
output.append(morse_code[c])
else:
output.append(" ")
print(" ".join(output))
return
elif task_type == 'decode':
message_array = []
word = ""
for c in range(len(message)):
if message[c] != " " :
word += message[c]
elif message[c] == " " and message[c-1] != " ":
message_array.append(word)
word = ""
elif message[c] == " " and message[c+1] == " ":
message_array.append(" ")
word=""
if c == len(message)-1:
message_array.append(word)
word = ""
for item in message_array:
for l in morse_code:
if item == morse_code[l]:
output.append(l)
break
elif item == " ":
output.append(" ")
break
print("".join(output))
return
else:
print('Wrong input recieved')
return
keep_converting = True
while keep_converting:
user_wish = input('Want to use morse code convertor?(y/n): ').lower()
if user_wish == 'y' or user_wish == 'yes':
text_to_morse_code()
elif user_wish == 'n' or user_wish == 'no':
keep_converting = False
break
else:
print('No comprehendable input entered.')
keep_converting = False
break