python转换摩斯密码示例

代码如下:

code = {‘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’: ‘—-.’ }def main(): msg = raw_input(‘message: ‘) for char in msg: if char == ‘ ‘: print else: print code[char.upper()] + ‘ ‘,if __name__ == “__main__”: main()