Welcome, guest | Sign In | My Account | Store | Cart

This is a slightly better version. It has the ability to translate sentences into Morse Code. I wrote this code for an article Morse Code and Dictionaries in Python

Python, 29 lines
 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
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': '--..',
        
        '0': '-----',  '1': '.----',  '2': '..---',
        '3': '...--',  '4': '....-',  '5': '.....',
        '6': '-....',  '7': '--...',  '8': '---..',
        '9': '----.' 
        }


def main():
	
	msg = raw_input('MESSAGE: ')
	
	for char in msg:
		if char == ' ':
			print ' '*7
		else:
			print CODE[char.upper()],
		
if __name__ == "__main__":
	main()
Created by Captain DeadBones on Tue, 8 Jan 2013 (MIT)
Python recipes (4591)
Captain DeadBones's recipes (47)

Required Modules

  • (none specified)

Other Information and Tasks