-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmorse.py
executable file
·88 lines (77 loc) · 2.58 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Morse code 2-way translator.
Author: Pairode Jaroensri
Last visited: May 8, 2017
"""
morse = {'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 get_option():
repeat = True
while(repeat):
print("Press 1 to translate from text --> Morse code.")
print("Press 2 to translate from Morse code --> text.")
a = input()
if(a == '1' or a == '2'):
repeat = False
return a
else:
print("Invalid option. Please try again.")
def get_key(value):
for k, v in morse.items():
if v == value:
return k
def encode(phrase):
phrase = phrase.upper() + " "
code = []
coded_word = ""
for char in phrase:
if char != ' ':
coded_word = coded_word + morse[char] + '/'
else: #char == ' ':
# Remove last character '/'
corrected_word = coded_word[:-1]
code.append(corrected_word)
coded_word = ""
return " ".join(code)
print("Welcome to Kim's Morse code translator V.2.10")
option = get_option()
if option == '1':
phrase = input("Enter your phrase: ")
try:
print("Your Morse code is: " + encode(phrase))
except Exception as e:
print("Invalid character: " + str(e))
elif option == '2':
print("Please enter in the following format:")
print("This is an example: -/..../../... ../... .-/-. ./-..-/.-/--/.--./.-../.")
code = input("Enter your Morse code here: ")
words = []
word = ""
letter = ""
for i in range(len(code)):
if code[i] == '.' or code[i] == '-':
letter = letter + code[i]
elif code[i] == '/':
letter = get_key(letter)
word = word + letter
letter = ""
elif code[i] == ' ':
letter = get_key(letter)
word = word + letter
words.append(word)
word = ""
letter = ""
else:
print("Invalid character.")
letter = get_key(letter)
word = word + letter
words.append(word)
word = ""
letter = ""
print("Your phrase is: " + " ".join(words))