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.

New Topic/Question
Reply



MultiQuote



|