Is there a way to type ".-" and produce "a"? (python) | Sololearn: Learn to code for FREE!

0

Is there a way to type ".-" and produce "a"? (python)

New to coding but had an idea. I'm trying to make a morse code translator. I can get it from the letter to morse but not the other way round. Current code example: A = ".-" B = "-..." [etc.] Print (A, B) Output: .- -... Any suggestions?

5/17/2020 4:00:54 AM

Morgan Ansell

3 Answers

New Answer

+2

Use a dictionary to store the values using the Morse strings as keys. d = {'.-': 'A', '-...': 'B'} morse = '.- -...'.split(' ') message = [] for c in morse: message.append(d[c]) print(''.join(message))

+1

Use dictionaries. They are collections of key-value pairs. #The string before ':' is the key and the string after is the value. morse = {"A" : ".-", "B" = "-..." , ".-" : "A", "-..." : "B"} #from letter to morse print(morse["A"] + morse["B"]) #from morse to letter print(morse[".-"] + morse["-..."] https://www.sololearn.com/learn/Python/2450/

+1

Thanks guys :)