# # Translate a string into Morse code; ignore anything other than letters, # numbers, period, and comma # # Matt Bishop, ECS 10, Fall 2012 # # dictionary mapping characters into Morse 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" : "----.", "." : ".-.-.-", "," : "--..--", "?" : "..--..", "'" : ".----.", "!" : "-.-.--", "/" : "-..-.", "(" : "-.--.", ")" : "-.--.-", "&" : ".-...", ":" : "---...", ";" : "-.-.-.", "=" : "-...-", "+" : ".-.-.", "-" : "-....-", "_" : "..--.-", '"' : ".-..-.", "$" : "...-..-","@" : ".--.-." } # unknown char: anything else is printed as this unk = "X" # separator to separate Morse representation of characters sep = "/" # # loop doing the translation until an EOF while True: # # read the input, quitting on an exception # try: inp = input("enter string: ") except EOFError: break except Exception as msg: # no idea what caused this, so give the error message print(msg) break # # now do the translation # note all letters get mapped into upper case # for c in inp: print(morse.get(c.upper(), unk), end=sep) print('')