gpt4 book ai didi

python - 很难通过 Python 3 制作莫尔斯到英语的翻译器

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:23 25 4
gpt4 key购买 nike

下面是我的英语莫尔斯翻译器(效果很好):

english = {'.-': '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', ' ': ' '}

print ("""


================================================================================
English to Morse Code Translator
================================================================================""")
time.sleep(2)
while True:
msg = input('''What would you like translated? please use lower-case letters.\n :''')
print ("")
for i in msg:
print(morse[i])
userstatus = input('Would you like to go again?(y or n): ').lower().strip(' ')
if userstatus not in('y', 'yes'):
print ("Have a nice day!")
break

现在一切正常,我遇到的问题是摩尔斯英语翻译器:

morse = {'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': '----.', ' ': ' '}


print ("""
================================================================================
Morse to English Code Translator
================================================================================""")
time.sleep(2)
while True:



msg = input('''What would you like translated?
:''')
print ("")
for i in msg:
print(english[i])
userstatus = input('Would you like to go again?(y or n): ').lower().strip(' ')
if userstatus not in('y', 'ye', 'yes'):
print ("Have a nice day!")
break

ans = input('''英语转摩尔斯语按1\n莫里斯语转英语按2 :''')

我遇到的问题是,一旦我输入一行摩尔斯电码,例如“-- --- .-. ... .”,它就会像这样打印出来:

    What would you like translated?
:-- --- .-. ... .

t
t

t
t
t

e
t
e

e
e
e

e
Would you like to go again?(y or n):

有谁知道我可以解决这个问题,所以它会打印出单词,在本例中是 morse,而不仅仅是 t 或 e?

最佳答案

在空格上拆分输入消息,然后遍历整个莫尔斯模式。一次迭代一个字符只会读取一个点或一个破折号。

这应该可以解决:

for i in msg.split():
print(english[i])

关于python - 很难通过 Python 3 制作莫尔斯到英语的翻译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37604086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com