Last active
October 27, 2024 23:11
-
-
Save StrangeRanger/4898b4882e9fece91cf9149874f5a6bb to your computer and use it in GitHub Desktop.
morse_code_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Version: v1.0.4 | |
# License: MIT License | |
# Copyright (c) 2020-2021 Hunter T. (StrangeRanger) | |
# | |
######################################################################################## | |
""" | |
Convert morse code to English and vise versa. | |
Notes | |
----- | |
The script can only convert one way. If you mix morse code and English in your | |
phrase/string, you will be asked to re-enter it. | |
""" | |
####[ Variables ]####################################################################### | |
translation = { | |
"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": "----- ", | |
".": ".-.-.- ", | |
",": "--..-- ", | |
"?": "..--.. ", | |
"/": "-..-. ", | |
"@": ".--.-. ", | |
" ": " ", | |
} | |
####[ Main ]############################################################################ | |
while True: | |
user_input = input("Enter your english or morse code message:\n").upper() | |
# Like saying, for c in user_input: if c in translation: ... | |
if all(c in translation for c in user_input): | |
morse = "".join(map(translation.get, user_input)) | |
print(morse) | |
break | |
if all(c in ".- " for c in user_input): | |
# v: k for k, v reverses/makes the variables change places. | |
trans_back = {v.rstrip(" "): k for k, v in translation.items()} | |
text = "".join(map(trans_back.get, user_input.split(" "))) | |
print(text) | |
break | |
print("Invalid input: enter a message only containing english or morse code\n") |
Author
StrangeRanger
commented
Jun 7, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment