Ser*_*ial 8 python dictionary morse-code
我正在制作一个程序,它接受输入并以计算机蜂鸣声的形式将其转换为莫尔斯代码,但我无法弄清楚如何制作它以便我可以在输入中放置多个字母而不会出现错误.
这是我的代码:
import winsound
import time
morseDict = {
'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': '--..'
}
while True:
inp = raw_input("Message: ")
a = morseDict[inp]
morseStr = a
for c in morseStr:
print c
if c == '-':
winsound.Beep(800, 500)
elif c == '.':
winsound.Beep(800, 100)
else:
time.sleep(0.4)
time.sleep(0.2)
Run Code Online (Sandbox Code Playgroud)
现在它一次需要一个字母,但我希望它能用短语.
只需添加一个额外的 for 循环并循环输入中的字符即可获取消息!但不要忘记在必要时结束循环!
在下面的代码中,我将消息解码后,询问您是否要发送另一条消息,如果您输入“n”,它将退出循环!
going = True
while going:
inp = raw_input("Message: ")
for i in inp:
a = morseDict[i]
morseStr = a
for c in morseStr:
print c
if c == '-':
winsound.Beep(800, 500)
elif c == '.':
winsound.Beep(800, 100)
else:
time.sleep(0.4)
time.sleep(0.2)
again = raw_input("would you like to send another message? (y)/(n) ")
if again.lower() == "n":
going = False
Run Code Online (Sandbox Code Playgroud)
现在你还有一个问题...你没有占空间!!所以你还是只能发文字!如果我是正确的,单词之间的空格是莫尔斯电码中固定定时的沉默,所以我想说你应该做的是添加:
" ": 'x'
Run Code Online (Sandbox Code Playgroud)
这样,当尝试查找空格实例时,它不会返回错误,并且它将在您的else语句中运行,并在下一个单词之前添加额外的 0.4 秒!
| 归档时间: |
|
| 查看次数: |
3395 次 |
| 最近记录: |