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)
Since I don’t write software for a living, and since I’m not in the IT business, per se, it can sometimes be hard for me to figure out what kinds of programs I should write. That’s why I’ve been slowly compiling a list of programs that might be interesting and fun to write. These should all be attainable at my current skill level…well, almost. I’m not saying they’ll be pretty, but hopefully functional. Here is what I have so far.