python娱乐项目:实现摩斯电码的加解密

今天不知为何有些烦躁,写个程序平静一下:

s = input("input the cipher_text or clear_text:")
choice = input("encode or decode? [1/0]:")
codebook = {
    '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':"--..",
    '1':".----",
    '2':"..---",
    '3':"...---",
    '4':"....-",
    '5':".....",
    '6':"-....",
    '7':"--...",
    '8':"---..",
    '9':"----.",
    '0':"-----",
    '.':".━.━.━",
    '?':"..--..",
    '!':"-.-.--",
    '(':"-.--.",
    '@':".--.-.",
    ':':"---...",
    '=':"-...-",
    '-':"-....-",
    ')':"-.--.-",
    '+':".-.-.",
    ',':"--..--",
    '\'':".----.",
    '_':"..--.-",
    '$':"...-..-",
    ';':"-.-.-.",
    '/':"-..-.",
    '\"':".-..-.",
    '&':"...."

}
clear = ""
cipher = ""
if choice == '1':
    s = s.upper()
    for c in s:
        if c == ' ':
            continue
        clear+=codebook[c]+" "
    print(clear)
else:
    ss = s.split(" ");
    for c in ss:
        for k in codebook.keys():
            if codebook[k] == c:
                cipher+=k
    print(cipher)

你可能感兴趣的:(Python)