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

This is a basic Morse Code Generator in python. I wrote it for an article I put together Morse Code and Dictionaries in Python

Python, 26 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
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:
		print CODE[char.upper()],
		
if __name__ == "__main__":
	main()

10 comments

Stuart Walsh 8 years, 8 months ago  # | flag

This program works fine in python code, but when made into and .exe file it takes the message but then in the wink of an eye it closes - too fast to see what the code example is. Could there a fix to this that is simple? If so, being a complete novice, could you provide that fix. I'm using pytoexe to make a file into an executable, and to prove it worked, another morse file with sound works fine when the message is translated it beeps to the code out.

Regards Stuart

Stuart Walsh 8 years, 8 months ago  # | flag

Keeps coming up with the error:

Traceback (most recent call last): File "H:\Education\SDD\Stu's Python Trials\MorsePracCode1.py", line 27, in <module> main() File "H:\Education\SDD\Stu's Python Trials\MorsePracCode1.py", line 24, in main print CODE[char.upper()], KeyError: ' '

Need this program desperately, if you could tell me where it goes wrong from that above it would be appreciated.

bob 7 years, 9 months ago  # | flag

it keeps telling me that there is a syntax error on the colon after the 'p', is there anything i could do?

Captain DeadBones (author) 7 years, 9 months ago  # | flag

bob, I have tried to download and run the code. It runs fine. Check you code, make sure you didn't copy it wrong.

bob 7 years, 8 months ago  # | flag

thanks, captain deadbones, i must have accidentally missed something, but it works fine now.

Colin Leitch 6 years, 9 months ago  # | flag

Would there be a way to make this do full words instead of just single letters?

Sam Mezzy 6 years, 5 months ago  # | flag

I made my own code, and I did the things like, 'A': '10', but I want to do a backwards translation, such as: '10': 'A' I get two errors that I don't quite understand. Help anyone? These are the errors: Traceback (most recent call last): File "C:\Users\samomezz\Desktop\srccode.py", line 23, in <module> main() File "C:\Users\samomezz\Desktop\srccode.py", line 20, in main print CODE[char.upper()], KeyError: '1'

CD 5 years, 6 months ago  # | flag

Here is an expanded morse code library with symbols included based on the International Telecommunications Union international standards as of 2009 (http://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1677-1-200910-I!!PDF-E.pdf)

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': '----.',

    ' ':'/', '.':'.-.-.-', ',':'--..--',
    ':':'---...', '?':'..--..', "'":'.----.',
    '-':'-....-', '/':'-..-.', '@': '.--.-.',
    '=':'-...-', '(':'-.--.', ')':'-.--.-',
    '+':'.-.-.'
    }
Dave Miller 4 years, 11 months ago  # | flag

I'm new at python. Can anyone tell me what this piece of code does?

if __name__ == "__main__": main()

S Saggi 4 years, 6 months ago  # | flag

At the end of the main section, you can add

raw_input("Press [ENTER] to exit.")

so that it will exit when you press enter

Finished code:

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:
        print CODE[char.upper()],
    raw_input("When done, press [ENTER]")

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