Skip to content
Snippets Groups Projects

Morse em Python

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Vytor Calixto
    morse.py 1.15 KiB
    import os
    
    # You need to install the sox library (apt-get install sox)
    def bell(duration, frequency):
    	os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (duration, frequency))	
    
    morseAlphabet ={
    	"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" : "--..",
        " " : "/"       
    }
    
    inverseMorseAlphabet=dict((v,k) for (k,v) in morseAlphabet.items())
    
    def encodeToMorse(message):
    	encodedMessage=""
    	for char in message[:]:
    		encodedMessage += morseAlphabet[char.upper()] + " "
    	return encodedMessage
    
    def bellMorseMessage(morse):
    	for char in morse[:]:
    		if(char == "."):
    			bell(0.3,440)
    		elif (char == "-"):
    			bell(0.789,220)
    
    def main():
    	msg = raw_input("Write your message: ")
    	morse = encodeToMorse(msg)
    	bellMorseMessage(morse)
    
    if __name__ == "__main__":
    	main()
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment