tomorse ={
        'A': '.-',              'a': '.-',
        'B': '-...',            'b': '-...',
        'C': '-.-.',            'c': '-.-.',
        'D': '-..',             'd': '-..',
        'E': '.',               'e': '.',
        'F': '..-.',            'f': '..-.',
        'G': '--.',             'g': '--.',
        'H': '....',            'h': '....',
        'I': '..',              'i': '..',
        'J': '.---',            'j': '.---',
        'K': '-.-',             'k': '-.-',
        'L': '.-..',            'l': '.-..',
        'M': '--',              'm': '--',
        'N': '-.',              'n': '-.',
        'O': '---',             'o': '---',
        'P': '.--.',            'p': '.--.',
        'Q': '--.-',            'q': '--.-',
        'R': '.-.',             'r': '.-.',
        'S': '...',             's': '...',
        'T': '-',               't': '-',
        'U': '..-',             'u': '..-',
        'V': '...-',            'v': '...-',
        'W': '.--',             'w': '.--',
        'X': '-..-',            'x': '-..-',
        'Y': '-.--',            'y': '-.--',
        'Z': '--..',            'z': '--..',
        '0': '-----',           ',': '--..--',
        '1': '.----',           '.': '.-.-.-',
        '2': '..---',           '?': '..--..',
        '3': '...--',           ';': '-.-.-.',
        '4': '....-',           ':': '---...',
        '5': '.....',           "'": '.----.',
        '6': '-....',           '-': '-....-',
        '7': '--...',           '/': '-..-.',
        '8': '---..',           '(': '-.--.-',
        '9': '----.',           ')': '-.--.-',
        ' ': ' ',               '_': '..--.-',
}

frommorse ={
        '.-': 'A',              '.-': 'a',              
        '-...': 'B',            '-...': 'b',
        '-.-.': 'C',            '-.-.': 'c',
        '-..': 'D',             '-..': 'd',
        '.': 'E',               '.': 'e',               
        '..-.': 'F',            '..-.': 'f',
        '--.': 'G',             '--.': 'g',  
        '....': 'H',            '....': 'h',
        '..': 'I',              '..': 'i',   
        '.---': 'J',            '.---': 'j',
        '-.-': 'K',             '-.-': 'k', 
        '.-..': 'L',            '.-..': 'l',
        '--': 'M',              '--': 'm', 
        '-.': 'N',              '-.': 'n', 
        '---': 'O',             '---': 'o',
        '.--.': 'P',            '.--.': 'p',
        '--.-': 'Q',            '--.-': 'q',
        '.-.': 'R',             '.-.': 'r', 
        '...': 'S',             '...': 's',
        '-': 'T',               '-': 't', 
        '..-': 'U',             '..-': 'u', 
        '...-': 'V',            '...-': 'v',
        '.--': 'W',             '.--': 'w', 
        '-..-': 'X',            '-..-': 'x',
        '-.--': 'Y',            '-.--': 'y', 
        '--..': 'Z',            '--..': '',  
        '-----': '0',           '--..--': ',',
        '.----': '1',           '.-.-.-': '.', 
        '..---': '2',           '..--..': '?',
        '...--': '3',           '-.-.-.': ';',
        '....-': '4',           '---...': ':', 
        '.....': '5',           '.----.': "'",
        '-....': '6',           '-....-': '-',
        '--...': '7',           '-..-.': '/',
        '---..': '8',           '-.--.-': '(',
        '----.': '9',           '-.--.-': ')',
        ' ': ' ',               '..--.-': '_',
              
}
text = raw_input('What is the translation you need?\n')
if text == char:
    morse_list = list()
    for char in text:
        for key in tomorse:
            if (char.lower() == key):
                value = tomorse[key]
                morse_list.append(value)
time= ' ' 
morse_out = ''
for item in morse_list:
    morse_out = morse_out + item + time
print'Your translation to morse is: \n'
print morse_out

else: 
    morse_list = list()
    for char in text:
        for key in frommorse:
                if (char.lower() == key):
                        value = frommorse[key]
                        morse_list.append(value)
time= ' ' 
morse_out = ' '
for item in morse_list:
	morse_out = morse_out + item + time
print 'Your translation to morse is: \n' 
print morse_out

This is my code ... it translate english to morse
but it keeps erroring and shows that when i input a, it will show et
eg a should print .- but when i input .- it print e t.

i just dont know what to do and its really fustrating PLEASE help ... :(
and how to loop this code ? O.o

Recommended Answers

All 8 Replies

put only lowercase to dictiomaries and access normally with []
What is char? Why time? How are you splitting morse words/characters?

put only lowercase to dictiomaries and access normally with []
What is char? Why time? How are you splitting morse words/characters?

i actually dont know ... sigh im screwed
could you help me ?..
oh and for this would just one morse dict be enough ?
cause i repeated it for some reason ..

if text == char: should yield an error since "char" has not been declared. If you want to test for letters it is easier to test for dot or dash as the if and use letters in the else (asuming there are no periods or dashes when letters are input).

## frommorse first
if "." in text or "-" in text:
# or
# if text[0] in frommorse:     ## assumes text does not start with dot or dash
    morse_list = list()
    for char in text:
        ## replace the following two statements with
        if char in frommorse:
        ##for key in frommorse:
                ##if (char.lower() == key):
                        value = frommorse[char]
                        morse_list.append(value)
        ## allow for errors in entry or in the dictionary (like no lower case "z")
        else:
            print char, "not in dictionary"

else:
    ## tomorse code

You can use
if char in string.letters:
as a test for letters vs. dots and dashes.

Print the dictionary, frommorse, somewhere in the code. A dictionary can not have multiple keys that are the same, so it does not contain what you think it does.

i actually dont know ... sigh im screwed
could you help me ?..
oh and for this would just one morse dict be enough ?
cause i repeated it for some reason ..

You can do:

text = raw_input('What is the translation you need?\n').lower()

and only include lower case in tomorse and

frommorse = dict((b,a) for a,b in tomorse.items())
tomorse ={
        'A': '.-',              'a': '.-',
        'B': '-...',            'b': '-...',
        'C': '-.-.',            'c': '-.-.',
        'D': '-..',             'd': '-..',
        'E': '.',               'e': '.',
        'F': '..-.',            'f': '..-.',
        'G': '--.',             'g': '--.',
        'H': '....',            'h': '....',
        'I': '..',              'i': '..',
        'J': '.---',            'j': '.---',
        'K': '-.-',             'k': '-.-',
        'L': '.-..',            'l': '.-..',
        'M': '--',              'm': '--',
        'N': '-.',              'n': '-.',
        'O': '---',             'o': '---',
        'P': '.--.',            'p': '.--.',
        'Q': '--.-',            'q': '--.-',
        'R': '.-.',             'r': '.-.',
        'S': '...',             's': '...',
        'T': '-',               't': '-',
        'U': '..-',             'u': '..-',
        'V': '...-',            'v': '...-',
        'W': '.--',             'w': '.--',
        'X': '-..-',            'x': '-..-',
        'Y': '-.--',            'y': '-.--',
        'Z': '--..',            'z': '--..',
        '0': '-----',           ',': '--..--',
        '1': '.----',           '.': '.-.-.-',
        '2': '..---',           '?': '..--..',
        '3': '...--',           ';': '-.-.-.',
        '4': '....-',           ':': '---...',
        '5': '.....',           "'": '.----.',
        '6': '-....',           '-': '-....-',
        '7': '--...',           '/': '-..-.',
        '8': '---..',           '(': '-.--.-',
        '9': '----.',           ')': '-.--.-',
        ' ': ' ',               '_': '..--.-',
}

frommorse ={
        '.-': 'A',              '.-': 'a',              
        '-...': 'B',            '-...': 'b',
        '-.-.': 'C',            '-.-.': 'c',
        '-..': 'D',             '-..': 'd',
        '.': 'E',               '.': 'e',               
        '..-.': 'F',            '..-.': 'f',
        '--.': 'G',             '--.': 'g',  
        '....': 'H',            '....': 'h',
        '..': 'I',              '..': 'i',   
        '.---': 'J',            '.---': 'j',
        '-.-': 'K',             '-.-': 'k', 
        '.-..': 'L',            '.-..': 'l',
        '--': 'M',              '--': 'm', 
        '-.': 'N',              '-.': 'n', 
        '---': 'O',             '---': 'o',
        '.--.': 'P',            '.--.': 'p',
        '--.-': 'Q',            '--.-': 'q',
        '.-.': 'R',             '.-.': 'r', 
        '...': 'S',             '...': 's',
        '-': 'T',               '-': 't', 
        '..-': 'U',             '..-': 'u', 
        '...-': 'V',            '...-': 'v',
        '.--': 'W',             '.--': 'w', 
        '-..-': 'X',            '-..-': 'x',
        '-.--': 'Y',            '-.--': 'y', 
        '--..': 'Z',            '--..': '',  
        '-----': '0',           '--..--': ',',
        '.----': '1',           '.-.-.-': '.', 
        '..---': '2',           '..--..': '?',
        '...--': '3',           '-.-.-.': ';',
        '....-': '4',           '---...': ':', 
        '.....': '5',           '.----.': "'",
        '-....': '6',           '-....-': '-',
        '--...': '7',           '-..-.': '/',
        '---..': '8',           '-.--.-': '(',
        '----.': '9',           '-.--.-': ')',
        ' ': ' ',               '..--.-': '_',
              
}

text = raw_input('What is the translation you need?\n')

if "." in text or "-" in text:
    value = tomorse[char]
    morse_list.append(value)
if (char.lower() == key):
    value = tomorse[key]
    morse_list.append(value)
if "." in text or "-" in text:
    morse_list = list()
    for char in text:
        if char in frommorse:
            value = frommorse[char]
            morse_list.append(value)
        else:
            print char, "not in dictionary"
else:
    morse_list = list()
    for char in text:
        for key in tomorse:
            if (char.lower() == key):
                value = tomorse[key]
                morse_list.append(value)
    time= ' ' 
    morse_out = ''
    for item in morse_list:
        morse_out = morse_out + item + time
    print 'Your translation to morse is: \n' 
    print morse_out

i got it like that now ..
its running smoothly but i dont know why it isnt printing what .- is or when i input dots or dashes in the program when it asks for something

I get these errors and have not tested further

Traceback (most recent call last):
File "./test_1.py", line 96, in <module>
value = tomorse[char]
NameError: name 'char' is not defined

Traceback (most recent call last):
File "./test_1.py", line 98, in <module>
if (char.lower() == key):
NameError: name 'char' is not defined

You can use a function instead of two separate blocks of code, maybe, but that is possibly because I don't understand how you separate receiving ".-"; is it "et" or "a", and if there is a space between, i.e. ". -" vs ".-" then you have to split the input first.

import string

def to_from(text, dict_in):
    for char in text:
        if char.lower() in dict_in:
            print "%6s" % (dict_in[char]),
        else:
            print char, "not in dictionary"
    print

tomorse ={'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': '----.',           ')': '-.--.-',
        ' ': ' ',               '_': '..--.-'
}
frommorse = dict((b,a) for a,b in tomorse.items())

text = raw_input('What is the translation you need?\n')
text=text.strip()
 
if text[0] in string.letters:
    to_from(text, tomorse)
else:
    to_from(text, frommorse)

I got it to work, after cutting out most of lines. I do not know how OK it is to post it but here it is anyway:

tomorse = {
        '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': '----.',
        ',': '--..--',     '.': '.-.-.-',    '?': '..--..',
        ';': '-.-.-.',     ':': '---...',    "'": '.----.',
        '-': '-....-',     '/': '-..-.',     '(': '-.--.-',
        ')': '-.--.-',     ' ': ' ',         '_': '..--.-'
}

frommorse = dict((b,a) for a,b in tomorse.items())

text = raw_input('What is the translation you need?\n').lower()
value = ''
if text.lstrip().startswith(('.','-')):
   # words with triple space
   for word in text.split('   '):
       # morse letter separated with single space
       for char in word.split():
          if char in frommorse:
             value += frommorse[char] + ' '
          else:
             print('Value for %r not found as morse.' % char)

   print('Your translation to text is:')
else:
    for char in text:
        if char in tomorse:
           value += tomorse[char] + ' '
        else:
           print('Value for %r not found as character.' % char)
    print('Your translation to morse is:')
# clean out last space
value = value.rstrip()
print(value)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.