2 Replies - 34517 Views - Last Post: 26 March 2011 - 07:51 AM Rate Topic: -----

#1 FuturoProgramador   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-January 10

String to Morse Code spacing difficulty

Posted 25 March 2011 - 08:22 PM

I'm writing a program that will convert a string, inputted by the user, to Morse code, with 1 space between each character of the Morse code (. or -), 3 spaces between each letter in Morse, and 7 spaces between each word in Morse. This is what I have so far:
def string2Morse():
    msg = input('Please enter a message: ')
    msg = msg.lower()
    morse = ''
    alpha = {'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':'- - . .','1':'. - - - -','2':'. . - - -',
             '3':'. . . - -','4':'. . . . -','5':'. . . . .','6':'- . . . .',
             '7':'- - . . .','8':'- - - . .','9':'- - - - .',
             '0':'- - - - -',' ':' '}
    for ch in msg:
        morse = morse + alpha[ch]
        if ch == ' ':
            morse = morse + '       '
        elif 97 < ord(ch) < 122:
            morse = morse + '   '
    print morse,



There is something wrong with my condition statements because it always returns the spacing between words in Morse with 11 spaces instead of 7. I really need help in fixing it so that only 7 spaces occur between each word. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: String to Morse Code spacing difficulty

#2 Ater Magus   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 20-January 10

Re: String to Morse Code spacing difficulty

Posted 25 March 2011 - 09:43 PM

The issue is in the logic. When you reach the last character of a word you add 3 spaces, next you add 1 space for the ' ' character, and then you added another 7 spaces for the word gap. My suggestion would be to move the morse = morse + alpha[ch] to the elif block and to reduce the number of spaces to 4 in the if block.
Was This Post Helpful? 2
  • +
  • -

#3 FuturoProgramador   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-January 10

Re: String to Morse Code spacing difficulty

Posted 26 March 2011 - 07:51 AM

View PostAter Magus, on 25 March 2011 - 10:43 PM, said:

The issue is in the logic. When you reach the last character of a word you add 3 spaces, next you add 1 space for the ' ' character, and then you added another 7 spaces for the word gap. My suggestion would be to move the morse = morse + alpha[ch] to the elif block and to reduce the number of spaces to 4 in the if block.


Thank you so much, this worked perfectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1