python text to morse translator breaks in mid-way

二次信任 提交于 2021-01-05 07:20:15

问题


As a practice in python, I tried to make a text to morse translator. but it breaks after executing only once. Could you help me figure out the problem? (It's probably in morse function) function beep is for sound, dictionary item_list is a pack of every character's special code.

import winsound
import time
def beep(char):
    if char == 'a':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'b':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'c':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'd':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'e':
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'f':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'g':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'h':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'i':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'j':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'k':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'l':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'm':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'n':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'o':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'p':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 'q':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'r':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 's':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == 't':
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'u':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'v':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'w':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'x':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'y':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == 'z':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == '1':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '2':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '3':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '4':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '5':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == '6':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == '7':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == '8':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == '9':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == '0':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '.':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '-':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '/':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == ',':
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
    elif char == '?':
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 500)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
        winsound.Beep(500, 150)
        time.sleep(150)
    elif char == ' ':
        print("\t")
    else:
        print("unknown")
        winsound.Beep(500, 1000)
        time.sleep(150)


item_dict = {
    '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': '--.. ',
    '0': '----- ',
    '1': '.---- ',
    '2': '..--- ',
    '3': '...-- ',
    '4': '....- ',
    '5': '..... ',
    '6': '-.... ',
    '7': '--... ',
    '8': '---.. ',
    '9': '----. ',
    ' ': '\t',
    '.': '.-.-.- ',
    ',': '--..-- ',
    '?': '..--.. ',
    '-': '-...- ',
    '/': '-..-. '
}


def morse():
    x = input("?")
    name_list = list(x)
    for x in name_list:
        print(item_dict[f"{x}"])
        beep(x)
    morse()


print('''text to morse-text
    enter the text you want''')


morse()

回答1:


From the comments, it should be sleep(.15)

Then, your beep function is too "painful" to watch, here is a suggestion:

import winsound
import time


def beep(char):
    """translates a char into morse-code dashes and dots, and beeps them"""
    sound = {'-': 500, '.': 150, ' ': 10}
    for dashdot in item_dict.get(char, ' '):
        if dashdot == '\t':
            dashdot = ' '
#         print(f'{dashdot}', end='')
        winsound.Beep(500, sound[dashdot])
        time.sleep(.15)
#     print(' ', end='')


item_dict = {
    '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': '--.. ',
    '0': '----- ',
    '1': '.---- ',
    '2': '..--- ',
    '3': '...-- ',
    '4': '....- ',
    '5': '..... ',
    '6': '-.... ',
    '7': '--... ',
    '8': '---.. ',
    '9': '----. ',
    ' ': '\t',
    '.': '.-.-.- ',
    ',': '--..-- ',
    '?': '..--.. ',
    '-': '-...- ',
    '/': '-..-. '
}


def morse():
    x = input()
    # x = item_dict.keys()
    name_list = list(x)
    for c in x:
        beep(c)

print('''text to morse-text
    enter the text you want''')

morse()

debug output:

.-  -...  -.-.  -..  .  ..-.  --.  ....  ..  .---  -.-  .-..  --  -.  ---  .--.  --.-  .-.  ...  -  ..-  ...-  .--  -..-  -.--  --..  -----  .----  ..---  ...--  ....-  .....  -....  --...  ---..  ----.    .-.-.-  --..--  ..--..  -...-  -..-.


来源:https://stackoverflow.com/questions/64964697/python-text-to-morse-translator-breaks-in-mid-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!