0% found this document useful (0 votes)
3 views

AIM1

Uploaded by

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

AIM1

Uploaded by

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

Topic: - Morse Code to English and English

to Morse code.
Summary of the Code

The program allows users to:

 Convert English text to Morse code.

 Convert Morse code back to English text.

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},")

 The program prompts the user to enter their name and


greets them.

Function: English to Morse Code:


python
def e_to_m(e):
m = ''
for c in e:
if c != ' ':
m += morse_code_dict[c.upper()] + ' '
else:
m += ' '
return m
 This function takes an English string e as input.
 It converts each character to its Morse code equivalent
using the dictionary, adding a space between Morse code
letters.
 Spaces in the input string are preserved.
Function: Morse Code to English:
python
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

 This function takes a Morse code string m as input.


 It splits the Morse code into individual codes and looks
them up in the dictionary to convert them back to English.
 If a Morse code matches a value in the dictionary, the
corresponding key (letter) is appended to the result.
 Main Loop:
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;else:
print("Invalid choice. Please try again.")

o The program enters a loop where it prompts the user


to choose an action (convert English to Morse, Morse
to English, or quit).
o Depending on the user's choice, it calls the
appropriate function and displays the results.
o The loop continues until the user inputs 'q' to quit.
OUTPUT: -
1.Taking the name from user

2.converting the English string to Morse code string.:

3.converting the Morse code into English string:

4,printing goodbye message


Conclusion
This code provides a straightforward interface for converting
between English and Morse code. It effectively utilizes a
dictionary for quick lookups and functions to handle the
conversions. If you have any specific questions or need further
elaboration on any part of the code, feel free to ask!

You might also like