Reputation: 15
the problem is that it's difficult to use the inputs as a key for the dictionary in a for loop , I tried to use tuple and list , but the same result
here's the code :
import re
morse = {
"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" : "----.",
"." : ".-.-.-",
"," : "--..--",
" " : " "
}
print("""
MORSECODE ENCYPTER """)
print("Enter the text to convert(keep in mind that upper case character, numbers , (.) and (,) are only allowed) :",end = '')
to_encrypt = input()
tuple1 = tuple( re.findall("." , to_encrypt) )
print (tuple1)
for i in tuple1 :
print(morse[tuple1])
when I enter the to_encrypt input (for examle H) it gives me :
Traceback (most recent call last):
File "x.py", line 50, in <module>
print(morse[tuple1])
KeyError: ('H',)
Upvotes: 1
Views: 2331
Reputation: 305
Primarily your for loop seems to be incorrect, you could probably try this out:
to_encrypt = list(str(input()))
for ch in to_encrypt:
morse_val = morse.get(ch, None)
if not morse_val:
print('could not encode ', ch)
else:
print(morse_val)
Let me know if you need better clarification.
P.S - Code above assumes you have defined the morse
dictionary. Also, I did not see the purpose of using regex in this.
Upvotes: 1