8

I'm making a program that takes input and coverts it to morse code in the form of computer beeps but I can't figure out how to make it so I can put more than one letter in the input without getting an error.

Here is my code:

import winsound
import time

morseDict = {
'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': '--..'
}
while True: 
    inp = raw_input("Message: ")
    a = morseDict[inp] 
    morseStr =  a
    for c in morseStr:
        print c
        if c == '-':
            winsound.Beep(800, 500)
        elif c == '.':
            winsound.Beep(800, 100)
        else:
            time.sleep(0.4)  
        time.sleep(0.2)

Right now it takes one letter at a time but I want it to take phrases.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Serial
  • 7,925
  • 13
  • 52
  • 71
  • 4
    +1 for already using a dictionary and for having a fun problem to solve. I never thought about making a morse code converter...FWIW, if you want to handle phrases, you'll probably need to add an entry for space in your dictionary as well. – mgilson May 03 '13 at 01:14

5 Answers5

2

Try changing your loop to something like this:

while True:
    inp = raw_input("Message: ")
    for char in inp:
        for x in morseDict[char]:
            print x
            if x == "-":
                winsound.Beep(800, 500)
            elif x == ".":
                winsound.Beep(800, 100)
            else:
                time.sleep(0.4)
            time.sleep(0.2)

That way you iterate first over the characters in the input, then you look up the character in morseDict and iterate over the value of morseDict[char].

Nolen Royalty
  • 18,415
  • 4
  • 40
  • 50
  • 1
    It seems like there should be some way to break from this infinite loop ... But, I guess this will just convert message after message after message. (never know when you want to know what something sounds like in morse code) – mgilson May 03 '13 at 01:17
  • @mgilson I agree that there should be a way to break, I just figured I'd stay true to op's code. – Nolen Royalty May 03 '13 at 01:18
  • that is true i will fix that in my program thanks for pointing that out ! – Serial May 03 '13 at 01:20
1

just add an extra for loop and loop through the characters in your input to get the message! But don't forget to end your loop when necessary!

In the following code I made it such that after the message has been decoded, it asks if you would like to send another, if you type "n" it will quit the loop!

going = True
while going: 
    inp = raw_input("Message: ")
    for i in inp:
        a = morseDict[i] 
        morseStr =  a
        for c in morseStr:
            print c
            if c == '-':
                winsound.Beep(800, 500)
            elif c == '.':
                winsound.Beep(800, 100)
            else:
                time.sleep(0.4)  
            time.sleep(0.2)
    again = raw_input("would you like to send another message? (y)/(n) ")
    if again.lower() == "n":
         going = False

now you still have one problem...you have not accounted for spaces!! So you can still only send words! If I am correct, a space between words is a fixed timed silence in morse code, so what I would say you should do is add:

" ": 'x'

this way it will not return an error when trying to find the instance of the space and it will run in your else statement and add an extra .4 seconds before the next word!

Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
1

I believe you need to iterate over the letters in input.

while True: 
    inp = raw_input("Message: ")
    for letter in inp:          # <- Edit in this line
        a = morseDict[letter]   # <- and this one, rest have increased indent
        morseStr =  a
        for c in morseStr:
            print c
            if c == '-':
                winsound.Beep(800, 500)
            elif c == '.':
                winsound.Beep(800, 100)
            else:
                time.sleep(0.4)  
            time.sleep(0.2)
        time.sleep(0.4)        # Or desired time between letters
Dr.Tower
  • 985
  • 5
  • 11
1

Replace this line:

a = morseDict[inp]

with this line:

a = ' '.join([morseDict[c] for c in inp])

That takes each character in your input string, looks up the morse equivalent, and concatenates the results together with a space separator (assuming you want that extra 0.4s delay between letters).

James Holderness
  • 22,721
  • 2
  • 40
  • 52
0

Try a first loop while the message has three empty spaces in the end for end the loop. Or a /n character. Its only a idea. Sorry for my bad english.