Morse Decoder 04-18-2013, 11:34 AM
#1
![[Image: A3kJLJb.png]](http://i.imgur.com/A3kJLJb.png)
This is a very simple python program that uses a dictionary containing Morse code key like '-' for T. The condition to decode via key present in the dictionary is:
Code:
store = ''
for items in code:
if the key is in the dictionary:
then store the value of the key in the variable Store
if else the key is not found in the dictionary:
print that the key was not found
print value of the variable store
Source Code:
Code:
morse = {'.-' : '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',}
store = ''
# We need to store the code with the spaces in between as intact so we use split() to keep the spaces between the codes and make a list code.
code = input('Input your Morse Code: ').split()
#For Error management
try:
#For each item in the list 'code'
for item in code:
#Get the value of the key and store it in the variable 'store', if the key exists in the dictionary, if not morse[item]
#will be false and it will raise the KeyError that key not found in the dictionary which will be handled
#as a normal error at the end of the code.
store = store + morse[item]
#Prints the decoded code stored in the variable 'store'
print('Your Decoded Message is: %s' % store)
# In case of the above code returns false due to morse[item]
except KeyError as error:
#Display error
print('Your code contains invalid morse code character', error)
Compiled Source Download: http://www.mediafire.com/?nujm7sgcxcliu97
Everything is explained in the code comments, Have fun!
If you like, you can make it encode the normal text into a morse, you just need to reverse the dictionary values and then ask for normal text
