Thanks to Yoshi for pointing out some redundancies I had in my code. In the original code that I was posting, I was using a “for” loop along with a counter. I was aware that the “for” loop acted as a counter itself, but I wasn’t aware that it actually took on the variable it was counting. This bit of information explains a lot. Thanks again Yoshi.
This is a better version, and less code.
#Python morse code translator
morse_code = {'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':'----.',' ':' | '}
#Function to translate string to morse code
def translate(data):
text = data.lower()
#guardian code
for i in text:
print morse_code[i] + ' ',
to_translate = raw_input("Type in the text that you would like
to translate into Morse code. ")
translate(to_translate)