0

I am making a small Morse Code translator, and everything works fine; nonetheless, the output is not shown properly.

CIPHER = {'E': "⦾", "A": '⦿', 'R': '⦾⦿', 'I': '⦿⦾', 'O': '⦿⦿',
        'T': '⦾⦾⦿', 'N': '⦾⦿⦾', 'S': '⦾⦿⦿', 'L': '⦿⦾⦾',
        'C': '⦿⦾⦿', 'U': '⦿⦿⦾', 'D': '⦿⦿⦿',
        'P': '⦾⦾⦾⦿', 'M': '⦾⦾⦿⦾', 'H': '⦾⦾⦿⦿',
        'G': '⦾⦿⦾⦾', 'B': '⦾⦿⦾⦿', 'F': '⦾⦿⦿⦾',
        'Y': '⦾⦿⦿⦿', 'W': '⦿⦾⦾⦾', 'K': '⦿⦾⦾⦿',
        'V': '⦿⦾⦿⦾', 'X': '⦿⦾⦿⦿', 'Z': '⦿⦿⦾⦾',
        'J': '⦿⦿⦾⦿', 'Q': '⦿⦿⦿⦾',
        '1': '⦾⦾⦾⦾⦿', '2': '⦾⦾⦾⦿⦿', '3': '⦾⦾⦿⦿⦿',
        '4': '⦾⦿⦿⦿⦿', '5': '⦿⦿⦿⦿⦿', '6': '⦿⦾⦾⦾⦾',
        '7': '⦿⦿⦾⦾⦾', '8': '⦿⦿⦿⦾⦾', '9': '⦿⦿⦿⦿⦾',
        '0': '⦿⦿⦿⦿⦿'
        }
def main():
    msg = input("Type your message below:\n\n")
    for char in msg:
        if char == ' ':
            print (' '*7,)
        elif char not in 'abcdefghijklmnopqrstuvwxyz1234567890':
            print ('')
        else:
            print (CIPHER[char.upper()])

I would expect the output for "Hello, World!" to be something like this:

⦾⦿⦾⦾⦿⦾⦾⦿⦿ ⦿⦿⦾⦿⦿⦾⦾⦿⦿⦿

However, the actual output looks much more like this:

⦾
⦿⦾⦾
⦿⦾⦾
⦿⦿



⦿⦿
⦾⦿
⦿⦾⦾
⦿⦿⦿

I tried removing and placing commas randomly. Then, I tried removing the '\n' statements on the input, but nothing changed with respect to the output. I tried to use the .splitlines as shown here (For loop outputting one character per line), but it stopped printing completely! Then, I googled it and did not found anything close to this problem, so I started to read more material on Python strings. I found a website (https://automatetheboringstuff.com/chapter6/) that has a good amount of material about Python strings, but I could not find anything that could solve my problem there.

I would greatly appreciate your help!

  • By the way, "char not in 'abcdefghijklmnopqrstuvwxyz1234567890':" could be replaced with "not char.isalnum()" – jackweb Jun 14 '17 at 17:04
  • I don't know much Morse, but I thought S and O were three dots/dashes (or vice versa). Your O is 2 characters and S has different chars. – Nick T Jun 14 '17 at 17:11
  • @jackweb thanks for the info, I just started using Python yesterday. –  Jun 14 '17 at 22:29
  • @NickT it is a variation of Morse. –  Jun 14 '17 at 22:30

2 Answers2

0

You seem to be accustomed to Python2 convention of using comma at the end of print arguments to prevent automatically adding newline. This is no longer working in Python3. You should use keyword argument end='' instead, like this: print (' '*7, end='')

Błotosmętek
  • 12,717
  • 19
  • 29
  • Thanks! I was not aware of that distinction. I applied it to all the print statements and it worked wonders! –  Jun 14 '17 at 22:25
0

Use

print(sth, end='')

to print without breaking line.

jackweb
  • 307
  • 4
  • 8