-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse.py
70 lines (63 loc) · 1.43 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
__author__ = 'Code Hobbits'
# Write a program to encrypt a message to morse code and decrypt it back to alphanumeric
# The length of a dot is one unit
# A dash is three units
# the space between parts of the same letter is one unit
# the space between letters is three units
# the space between words is seven units
import sys
import re
morseCode = {
'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 encode(m):
charpos = 0
s = ""
while charpos < len(m):
letter = m[charpos]
# if the letter is not found in morseCode dictionary, exit python
if letter not in morseCode:
sys.exit("Character cannot be translated to Morse Code")
s = s + morseCode[letter] + " "
charpos += 1
return s
print("Enter the message you want to encode: ")
message = input().upper()
secretMessage = encode(message)
print(secretMessage)