摩尔斯电码的文字

时间:2017-06-05 10:46:25

标签: python

我最近发现你可以在python中播放声音。我立即想把一个文本写成莫尔斯代码程序,但当我尝试将winsound.Beep(600,300)分配给" a"并且发射时,它只会发出一次哔哔声,当我输入它时它什么也没做。

import winsound
def dot():
    winsound.Beep(600,100)
def line():
    winsound.Beep(600,100)
a = dot(), line()    #it doesn't work when i just do
                     #a = winsound.Beep(600,100) either
                     #and it beeps at the beginning which i don't want

你能告诉我如何将winsound.Beep()分配给变量吗?

4 个答案:

答案 0 :(得分:3)

你正在混合#34;现在运行的代码"使用"代码我想稍后运行"。

你的行,

a = dot(), line()

运行dot()(发出哔哔声并返回None)然后运行line()(发出哔哔声并返回None)然后将结果(None, None)分配给变量a

而是尝试

def a():
    dot()
    line()

创建一个函数,您可以稍后通过调用a()来运行...但它仍然会听起来像一个连续的蜂鸣声,因为您需要在{{1的定义中的每个音调后添加一个短暂的暂停}和dot()。您可能会发现line()有用,或者可能time.sleep()已经提供类似的内容。

修改

您可以使用调度表(将字符转换为函数调用的字典)和输入循环,如下所示:

winsound

答案 1 :(得分:1)

我对您的问题的解决方案将涉及将函数作为参数传递给其他函数。如果您对此不熟悉,我建议您稍微研究一下Python的函数式编程。

import winsound
import time

# first - create the `dot` and `line` functions
def dot():
    winsound.Beep(600, 100)
    time.sleep(0.1)               # this is in seconds

def line():
    ....

def space():
    ....

def full_stop():
    ....

# now you need some way of mapping letters to sounds
mapping = {
    "a": (dot, line),
    "b": (line, dot, dot, dot),
    ....
    " ": (space,),                 # A one-tuple is defined like this, not like `(var)`
    ".": (full_stop,)
}

# And as a final step you need a function to play the sounds
def play_morse(message):
    for character in message:
        character = character.lower()
        if character in mapping:
            for func in mapping[character]:
                func()
        else:
            print("Unknown character: '{}'".format(character))

您可以使用如下函数:

>>> play_morse("Hello world!")

答案 2 :(得分:0)

添加间接层通常很有帮助。与播放分开定义字符值,然后您可以遍历任何文本:

import winsound
import time

# define the values for the alphabet (using easy to verify strings)
alphabet = dict(
    a='.-',
    b='-...',
    c='-.-.',
    d='-..',
    e='.',
    h='....',
    l='.-..',
    o='---',
    w='.--',
    r='.-.',
    # etc.
)

如何播放(单个)点和短划线(这些线称为破折号):

def play_dot():
    winsound.Beep(600, 100)

def play_dash():
    winsound.Beep(500, 200)  # a bit lower frequency, double length

通过查找alphabet中字符的莫尔斯值来播放一个字符,然后迭代点/短划线:

def play_char(ch):
    if ch == ' ':             # space is a special case
        time.sleep(0.9)       # wait for 0.9 seconds before returning
        return '<space>'

    morseval = alphabet[ch]   # morseval is now a series of dots/dashes
    for d in morseval:        # loop over them
        if d == '.':          # if it's a dot, play a dot
            play_dot()
        else:
            play_dash()
        time.sleep(0.1)       # a small break (0.1 secs) makes it easier on the ears

    return morseval           # return the morse value so we can see what was played.. 

播放文本,假设它是ascii,只需遍历字符并依次播放每个字符:

def txt2morse(txt):
    for ch in txt.lower():    # convert the text to lower case, just in case..
        print play_char(ch),  # Python 2.7'ism to keep printing on the same line
    print

然后:

>>> txt2morse('hello world')
.... . .-.. .-.. --- <space> .-- --- .-. .-.. -..

答案 3 :(得分:0)

在python中,一切都是对象。如果你运行这个:

a = dot()

执行dot()并将返回的值分配给a - 在您的情况下为None

但是,如果您要将功能dot()“分配”到a,然后拨打a,请执行以下操作:

a = dot
a()

在这种情况下,名称adot指的是同一个对象。 a现在是一个只用括号调用的函数。