I'm trying to figure out how to make my program pause. The method encodeMesssage
is working fine and turns each letter into it's corresponding Morse code letter. Once it reaches the end of a letter it turns gives a letter pause [lpause]
. Then after it finishes a complete word it will give a word pause [wpause]
.
My second method is supposed to turn this message into actually sounds with the windsound module. The beeps are working fine, my problem is that I can't seem to get the [lpause]
and [wpause]
to behave.
morseCode = {'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':'-----' }
def encodeMessage(m):
message = m.upper().strip()
encodedMessage =''
isInWord = False
for ch in message:
if isInWord:
if ch in morseCode:
encodedMessage += '[lpause]'+ morseCode[ch]
else:
encodedMessage += '[wpause]'
isInWord = False
else: # not in word
if ch in morseCode:
encodedMessage += morseCode[ch]
isInWord = True
else:
pass # nothing to do
return encodedMessage
def sendMoreCodedMessage(message):
for ch in message:
if ch == '.':
winsound.Beep(200, 100)
elif ch == '-':
winsound.Beep(370, 100)
else:
time.sleep(1)
return None
here is an example of the output from encodeMessage
....[lpause].[lpause]-.--[wpause]-.[lpause]---[lpause].--[wpause]-.--[lpause]---[lpause]..-[wpause].-[wpause]-..[lpause].-.[lpause]---[lpause].--[lpause]-.[wpause]-.-.[lpause]---[lpause].--' –
I think you should probably use one-character identifiers for your letter and word pauses, since you use per-character checks to figure out what sound to make. I have replaced the pause identifiers with 'l' and 'w', and adjusted your SendMoreCodedMessage function accordingly:
def encodeMessage(m):
message = m.upper().strip()
encodedMessage =''
isInWord = False
for ch in message:
if isInWord:
if ch in morseCode:
encodedMessage += 'l'+ morseCode[ch]
else:
encodedMessage += 'w'
isInWord = False
else: # not in word
if ch in morseCode:
encodedMessage += morseCode[ch]
isInWord = True
else:
pass # nothing to do
return encodedMessage
def sendMoreCodedMessage(message):
for ch in message:
if ch == '.':
winsound.Beep(200, 100)
elif ch == '-':
winsound.Beep(370, 100)
elif ch == 'w':
time.sleep(3)
elif ch == 'l':
time.sleep(1)
return None