def encode(message):
morse = []
for letter in message:
morse.append( letter_to_morse[letter.lower()] )
return morse
encode(message) == morse
True
encode("Hello World")
['....', '.', '.-..', '.-..', '---', '/', '.--', '---', '.-.', '.-..', '-..']
morse_to_letter = {}
for letter in letter_to_morse.keys():
morse_to_letter[ letter_to_morse[letter] ] = letter
def decode(morse):
english = []
for code in morse:
english.append( morse_to_letter[code] )
return "".join(english)
decode(morse)
'sos we have hit an iceberg and need help quickly'
for message in messages:
print("checking for message '%s'..." % message)
print( message == decode( encode(message) ) )
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-26-63398adc5898> in <module> ----> 1 for message in messages: 2 print("checking for message '%s'..." % message) 3 print( message == decode( encode(message) ) ) NameError: name 'messages' is not defined
for message in messages:
print("checking for message '%s'..." % message)
print( message.lower() == decode(encode(message)) )
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-27-637c5861e8e5> in <module> ----> 1 for message in messages: 2 print("checking for message '%s'..." % message) 3 print( message.lower() == decode(encode(message)) ) NameError: name 'messages' is not defined