Skip to content

Instantly share code, notes, and snippets.

@GrantBGreat
Last active March 17, 2021 00:37
Show Gist options
  • Save GrantBGreat/3e0fda8cd1ca73ab781ee053e9b51ebf to your computer and use it in GitHub Desktop.
Save GrantBGreat/3e0fda8cd1ca73ab781ee053e9b51ebf to your computer and use it in GitHub Desktop.
Simple translator to Morse code
# get the input as English text
raw_text = input("Please type in your text input:\n")
# This is the dictionary I created to relate English text to Morse text
morse = {"-":"T", "/":"-..-.", " ":"/", ".":".-.-.-", "1":".----", "2":"..---", "3":"...--", "4":"....-", "5":".....", "6":"-....", "7":"--...", "8":"---..", "9":"----.", "0":"-----", "!":"-.-.--", "@":".--.-.", "&":".-...", "(":"-.--.", ")":"-.--.-", "=":"-...-", "+":".-.-.", "q":"--.-", "w":".--", "e":".", "r":".-.", "t":"-", "y":"-.--", "u":"..-", "i":"..", "o":"---", "p":".--.", "a":".-", "s":"...", "d":"-..", "f":"..-.", "g":"--.", "h":"....", "j":".---", "k":"-.-", "l":".-..", ":":"---...", '\'':".----.", "\"":".-..-.", "z":"--..", "x":"-..-", "c":"-.-.", "v":"...-", "b":"-...", "n":"-.", "m":"--", ",":"--..--", "?":"..--..s"}
# parse the text while converting it to morse code
morse_text = raw_text.lower()
for key, value in morse.items():
morse_text = morse_text.replace(key, f"{value} ")
# print the result
print(f"\n\"{raw_text}\" in morse code is:\n{morse_text}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment