This is a basic Morse Code Generator in python. I wrote it for an article I put together Morse Code and Dictionaries in Python
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()
|
Tags: morse_code, python
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
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.
it keeps telling me that there is a syntax error on the colon after the 'p', is there anything i could do?
bob, I have tried to download and run the code. It runs fine. Check you code, make sure you didn't copy it wrong.
thanks, captain deadbones, i must have accidentally missed something, but it works fine now.
Would there be a way to make this do full words instead of just single letters?
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'
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': '--..',
I'm new at python. Can anyone tell me what this piece of code does?
if __name__ == "__main__": main()
At the end of the main section, you can add
so that it will exit when you press enter
Finished code: