注册 登录
编程论坛 Python论坛

请教!下面的代码为什么不能将摩尔斯电码转换为英语原文?

yitian1368 发布于 2022-12-01 14:45, 563 次点击
code=["._","_...","_._.","_..",".",".._.","__.","....","..",".___","_._","._..","__","_.","___",".__.","__._","._.","...","_",".._","..._",".__","_.._","_.__","__.."]
message=input("请输入电报:")
message+=""
chars=""
output=""
for letter in message:
    if letter!="":
        chars=chars+letter
    else:
        for index in range(26):
            if code[index]==chars:
                output+=chr(ord('A')+index)
        chars=""
print("英语原文是:",output)
            
1 回复
#2
wp2319572022-12-07 14:22
回复 楼主 yitian1368
输入报文是啥格式的   码与码之间  用什么隔开

程序代码:

code=["._","_...","_._.","_..",".",".._.","__.","....","..",".___","_._","._..","__","_.","___",".__.","__._","._.","...","_",".._","..._",".__","_.._","_.__","__.."]
passwd='._   _...   _._.   _..   .   .._.   __.   ....   ..   .___   _._   ._..   __   _.   ___   .__.   __._   ._.   ...   _   .._   ..._   .__   _.._   _.__   __..'
source=""
for x in passwd.split():
    if x in code:
        source+=chr(ord("A")+code.index(x))
print(source)        
1