if __name__ == '__main__':
use this guard that allows you to import from this script without running the code.
if __name__ == '__main__':
print(convert_to_morse_code('HI MICHAEL'))
print(convert_to_word('.... .. -- .. -.-. .... .- . .-..'))
Style
check PEP0008 https://www.python.org/dev/peps/pep-0008/ the official Python style guide and here are a few comments:
Docstrings: Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. An object's docstring is defined by including a string constant as the first statement in the object's definition. Use docstrings in the following way to indicate what your functions do:
def convert_to_morse(text):
"""Convert text to Morse code."""
# code goes here
def convert_morse_to_word(morse_code):
"""Convert Morse code to alphanumeric words."""
# code goes here
Blank lines use blank lines sparingly (too many blank lines in your script)
string = ''
if ' ' in word:
word = word.replace(' ', '</<')
if ' ' in word:
word = word.replace(' ', '<')
for code in word:
if code == '/':
word = word.replace(code, ' ')
word = word.split('<')
for code in word:
for key in morse_code.keys():
if morse_code[key] == code:
string += key
return string
Code
f- strings provide a way to embed expressions inside string literals, using a minimal syntax and are not supposed to be used this way:
string += f'{morse_code[letternum.upper()]} '
could be written:
string += morse_code[letternum.upper()]
isalnum() a bad idea if you want to include punctuation and spaces
and Morse code does not exclude punctuation btw.
- string += this is inefficient because strings cannot be changed in place so each time you add to the string, a new string is created. A better approach is to use list comprehensions and join the results.
We have to reconstruct dictionary based on the following international Morse code:

and the code looks like:
def get_translation(translate_to):
"""
Return a dictionary from to (Morse code-text)
assuming translate_to a string:
m for translation to Morse
t for translation to text.
"""
morse_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': '--..',
'á': '.--.-', 'ä': '.-.-', 'é': '..-..', 'ñ': '--.--', 'ö': '---.', 'ü': '..--', "'": '·----·',
'1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----', '!': '-·-·--',
'.': '.-.-.-', ',': '--..--', '?': '..--..', ':': '---...', "\"": '.-..-.',
'-': '-....-', '/': '-..-.', '(': '-.--.', ')': '-.--.-', ' ': '\t', '\n': '\t', '_': '··--·-'
}
morse_to_letter = {code: letter for letter, code in morse_code.items()}
if translate_to == 'm':
return morse_code
if translate_to == 't':
return morse_to_letter
else:
raise ValueError(f'Invalid input{translate_to} expected m or t')
def convert_to_morse_code(text):
"""Translate text to Morse code."""
morse_code = get_translation('m')
return ' '.join([morse_code[letter] for letter in text.lower()])
def convert_morse_code_to_text(morse_code):
"""Translate from Morse code to text."""
morse_to_letter = get_translation('t')
text = []
words = morse_code.split('\t')
for word in words:
letters = word.split()
to_text = [morse_to_letter[letter] for letter in letters]
text.append(''.join(to_text))
return ' '.join(text)
if __name__ == '__main__':
print(convert_to_morse_code('Hello Michael! How are you doing today?'))
print(convert_morse_code_to_text('·· ·----· -- ..-. ·· -· . - ···· ·- -· -.- ... .-.-.-'))