-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMorse Translator
209 lines (162 loc) · 5.66 KB
/
Morse Translator
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# necessary module imports
import winsound
import sys
from time import sleep
# The Morse Code alphabet to translate to and from
morse_alph_a = {
'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': '----.',
'.': '.-.-.-', ',': '--..--', ':': '---...', '?': '..--..',
"'": '.----.', '-': '-....-', '//': '-..-.', '(': '-.--.-',
')': '-.--..', '"': '.-..-.', '@': '.--.-.', '=': '-...-', ' ': ' '
}
morse_alph_b = dict(zip(morse_alph_a.values(), morse_alph_a
.keys()))
#
# Begin defining function's #
#
def menu():
"""
Summary: Presents the user with a menu giving the option of a
direct translation, translation from a file, or quiting. If there
is an invalid input the function will be called again so the user
can make another selection
Arguments: None
Returns: None
"""
print('Do you want to:\n\
1) Translate from a file\n\
2) Translate from direct input\n\
9) quit the program')
choice = input('Option:')
if choice == '1':
morse_code = translate_from_file()
print(morse_code)
play_morse(morse_code)
to_write = input(
'Do you want to write this Morse code to a text file?').lower()
if to_write == 'yes' or to_write == 'y':
print('RAN')
write_to_file(morse_code)
elif choice == '2':
morse_code = translate_directly()
print(morse_code)
play_morse(morse_code)
to_write = input(
'Do you want to write this morse code to a text file?').lower()
if to_write == 'yes' or to_write == 'y':
write_to_file(morse_code)
elif choice == '9':
quit()
else:
print('invalid choice')
menu()
def detect_translate(user_input):
"""
Summary: Decides if a user input is in Morse code or not
Arguments: user_input - the users inputed string
Returns: morse or english
"""
dit_dar_count = 0
for i in user_input:
if i == '-' or i == '.':
dit_dar_count += 1
print('dit_dar_count =',dit_dar_count)
print(len(user_input) / 2)
if len(user_input) / 2 < dit_dar_count:
return 'morse'
else:
return 'english'
def translate_from_file():
# read_from_file is ran then verified this information is passed to the
# translate function.
user_input = read_from_file()
if verify_input(user_input):
print('You tried to translate Invalid characters')
translate_from_file()
else:
pass
return (translate(user_input, detect_translate()))
def translate_directly():
# get_user_input is ran this information is verified and passed to the
# translate function
user_input = get_user_input()
if verify_input(user_input):
print('You tried to translate Invalid characters')
translate_from_file()
else:
pass
return (translate(user_input, detect_translate(user_input)))
def read_from_file():
"""
Summary: Prompts the user to enter a filename. The last 4 char's are
checked to see if they are .txt if they are not .txt is appended
to the end. This sets the f variable and the file is read and the
value stored in user_input. This is then capitalized and returned.
Arguments: None
Returns: user_input(An Upper case string)
"""
print('NOTE: FILE MUST BE IN SAME DIRECTORY AS THIS PROGRAM')
filename = input('Enter the filename to read from: ').lower()
if filename[-4:] != '.txt':
filename += '.txt'
f = open(filename, 'r')
user_input = f.read()
f.close()
return user_input.upper()
def get_user_input():
user_input = input('Enter text to be translated:')
return user_input.upper()
def verify_input(user_input):
test_bool = False
for char in user_input:
if char in morse_alph_a:
pass
else:
test_bool = True
break
return test_bool
def translate(user_input, type):
string = ''
if type == 'english':
for letter in user_input:
string += morse_alph_a[letter]
string += ' '
elif type == 'morse':
for letter in user_input:
string += morse_alph_b[letter]
string += ' '
else:
raise NameError('invalid type in translate function')
return(string)
def play_morse(morse_code):
for char in morse_code:
if char is '.':
winsound.Beep(700, 100)
elif char is '-':
winsound.Beep(700, 300)
elif char is '/':
sleep(0.2)
sleep(0.05)
def write_to_file(morse_code):
print('NOTE: IF YOU HAVE A FILE WITH THE SAME NAME IT WILL BE \n\
OVERWTITTEN IF YOU DON\'T THE FILE WILL BE CREATED')
filename = input('Enter the filename to write to: ').lower()
if filename[-4:] != '.txt':
filename += '.txt'
f = open(filename, 'w')
print(morse_code)
user_input = f.write(morse_code)
f.close()
def main():
while True:
menu()
if __name__ == '__main__':
main()