6 Comments
User's avatar
Yuvraj Raghav's avatar

this is my solution:

user_input = input("Enter Text: ")

user_input = user_input.upper().strip()

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': '----.',

'.': '.-.-.-', ',': '--..--', '?': '..--..', '/': '-..-.',

'-': '-....-', '(': '-.--.', ')': '-.--.-', '&': '.-...',

':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.',

'"': '.-..-.', '$': '...-..-', '!': '-.-.--', '@': '.--.-.'

}

morse_code=[]

for char in user_input:

if char==" ":

morse_code.append('/')

else:

morse_code.append(MORSE_CODE_DICT[char])

print(" ".join(morse_code))

Bijal's avatar

I reviewed the solution, but I'm not quite sure why .strip() method to print the Morse code in the solution part-1 is necessary. Could you please help clarify it for me?

I checked the output, I didn't saw any difference.

Ardit Sulce's avatar

Hi Bijal, without strip() there will be a space at the end of the printed morse code. This is not apparent, but if you select the printed code you will find the space. The strip() method removes that space at the end.

riley's avatar

where do I wright the code

Ardit Sulce's avatar

You can write it in your local IDE and then simply compare it with the solution we have provided.