【问题标题】:python text to morse translator breaks in mid-way莫尔斯翻译器的python文本在中途中断
【发布时间】:2021-03-05 22:46:03
【问题描述】:

作为python的练习,我尝试将文本制作成莫尔斯翻译器。但它只执行一次后就中断了。你能帮我找出问题所在吗? (可能是莫尔斯函数)函数beep是声音,字典item_list是每个字符的特殊代码包。

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()

【问题讨论】:

  • 为什么每个人都认为他们需要在使用for-in 循环之前将字符串转换为列表?
  • 确实没有必要在您的问题中添加您的电子邮件地址,更不用说您的 Instagram 帐户了。
  • 既然x已经是一个字符串,就不用写f"{x}"了,直接写x就行了。
  • 不要使用所有那些if 语句,您应该循环遍历item_dict() 中的字符,然后发出适当长度的点或破折号。
  • 你能更好地描述这个问题吗?它是在第一个字符之后停止,还是在第一次调用 morse() 之后停止?

标签: python python-3.x terminal morse-code


【解决方案1】:

来自cmets,应该是sleep(.15)

那么,你的beep函数太“痛苦”看不下去了,给个建议:

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()

调试输出:

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

【讨论】:

  • 痛苦 :) 感谢您的帮助。
  • 大声笑-好的,我把它放在引号里! ;-)
  • 如果您开始使用 c 限制进行编程,您的答案就不会出现在您的脑海中。这些组合就像 c 中的一个笑话。还不适合 py。
  • 他的item_dict 有空格' '..我没有删除所有空格,而是添加了修改为sound = {'-': 500, '.': 150,' ':0}
  • 我在空格处收到错误KeyError: '\t'...嗯,如何解决?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 2012-11-05
  • 2014-09-18
相关资源
最近更新 更多