Add Morse Code Translator

This commit is contained in:
nabroleonx 2022-10-15 01:04:30 +03:00
parent 03a4784eaa
commit 7e8a1b9b63
2 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,106 @@
MORSE_CODES = {
"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": "-----",
", ": "--..--",
".": ".-.-.-",
"?": "..--..",
"/": "-..-.",
"-": "-....-",
"(": "-.--.",
")": "-.--.-",
}
def encrypt(message):
cipher = ""
for letter in message:
if letter != " ":
cipher += MORSE_CODES[letter] + " "
else:
cipher += " "
return cipher
def decrypt(message):
message += " "
decipher = ""
citext = "" # stores morse code of a single character
for letter in message:
if letter != " ":
i = 0 # keeps count of the spaces between morse characters
citext += letter
else:
i += 1
if i == 2:
decipher += " "
else:
decipher += list(MORSE_CODES.keys())[
list(MORSE_CODES.values()).index(citext)
]
citext = ""
return decipher
def main():
while True:
print('''
-----------------------------
Morse Code Translator
1. Encrypt
2. Decrypt
Enter 'q' to quit.
-----------------------------
''')
choice = input("Enter your choice: ")
if choice == "1":
message = input("Enter message to encrypt: ")
result = encrypt(message.upper())
print("> ", result)
elif choice == "2":
message = input("Enter message to decrypt: ")
result = decrypt(message)
print("> ", result)
elif choice == "q":
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,25 @@
# Morse Code Translator
This simple script translates texts into morse code and vise versa.
## Usage
It has two options:-
1). Encrypt a text into a morse code.
2). Decrypt a morse code into a text.
Just run it and follow the instructions. Here is a simple example.
```shell
-----------------------------
Morse Code Translator
1. Encrypt
2. Decrypt
Enter 'q' to quit.
-----------------------------
Enter your choice: 1
Enter message to encrypt: Metafy social
> -- . - .- ..-. -.-- ... --- -.-. .. .- .-..
```