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?
3 Réponses
+ 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/JUMP_LINK__&&__Python__&&__JUMP_LINK/2450/
+ 1
Thanks guys :)