0% found this document useful (0 votes)
9 views1 page

Morse

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Morse

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

MORSE_CODE_DICT = {

'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 text_to_morse(text):
morse_text = []
for char in text.upper():
morse_text.append(MORSE_CODE_DICT.get(char, ''))
return ' '.join(morse_text)

def morse_to_text(morse_code):
text = []
morse_words = morse_code.split(' / ')
for word in morse_words:
for code in word.split():
for char, morse in MORSE_CODE_DICT.items():
if morse == code:
text.append(char)
break
text.append(' ')
return ''.join(text).strip()

print("Pilih opsi konversi:")


print("1. Teks ke Morse")
print("2. Morse ke Teks")

choice = input("Masukkan pilihan (1 atau 2): ")

if choice == '1':
teks = input("Masukkan teks: ")
morse = text_to_morse(teks)
print("Hasil Morse:", morse)
elif choice == '2':
morse_code = input("Masukkan kode Morse (gunakan '/' untuk spasi antar kata):
")
teks_dari_morse = morse_to_text(morse_code)
print("Hasil Teks:", teks_dari_morse)
else:
print("Pilihan tidak valid.")

You might also like