AIM1
AIM1
to Morse code.
Summary of the Code
Source code: -
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': '--..', '0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.', '': '/'
}
a=str(input("Enter your Name: "))
print(f"Hello {a},")
def e_to_m(e):
m = ''
for c in e:
if c != ' ':
m += morse_code_dict[c.upper()] + ' '
else:
m += ' '
return m
def m_to_e(m):
e = ''
for code in m.split():
for k, v in morse_code_dict.items():
if code == v:
e += k
return e
while True:
print("Press \n1 for English to Morse \n2 for Morse to English \
n'q' to quit:")
c = input().lower()
if c == '1':
e = input("Enter the English message: ")
m = e_to_m(e)
print("English: " + e)
print("Morse: " + m)
elif c == '2':
m = input("Enter the Morse message: ")
e = m_to_e(m)
print("Morse: " + m)
print("English: " + e)
elif c == 'q':
print("Goodbye!")
break;
Code Breakdown
1. Morse Code Dictionary:
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': '--..', '0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.', '': '/'
}
This dictionary maps each letter and numeral to its
corresponding Morse code representation.
User Input:
python
a = str(input("Enter your Name: "))
print(f"Hello {a},")