my name is bishop
I am a python programmer
I am a writer
I practice zen meditation
I am a musician
I am in peak physical health
I am a photographer
I speak fluent russian


and I do not exist.........yet

Morse code translator (take two)

March 26th, 2008 by Robert

PythonThanks 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)

Posted in python | 4 Comments »

Morse code translator

March 21st, 2008 by Robert

PythonI finally got around to whipping this up. It’s crude, but it’s a start. The next thing I need to do is fix the code tags for coldspell. They’re not behaving like good little tags should.

#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

    count = 0
    for i in text:
        print morse_code[text[count]] + '  ',
        count += 1

to_translate = raw_input("Type in the text that you would like
                             to translate into Morse code.  ")

translate(to_translate)

Posted in python | 4 Comments »