def morse_code_encryption(txt): """接收明文字符串为参数,返回用摩斯密码加密后的字符串。""" # 补充你的代码 if __name__ == '__main__': plaintext = input().lower() # 输入一个字符串并转为小写 print(morse_code_encryption(plaintext)) # 调用函数,并输出返回值
时间: 2024-01-08 18:04:32 浏览: 30
以下是一个简单的实现,将每个字符转换为摩斯码,然后用空格分隔每个字符的摩斯码,再用两个空格分隔每个单词:
```python
MORSE_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 morse_code_encryption(txt):
encrypted_text = []
for char in txt:
if char in MORSE_CODE:
encrypted_text.append(MORSE_CODE[char])
return ' '.join(encrypted_text).replace(' ', ' ')
if __name__ == '__main__':
plaintext = input().lower()
print(morse_code_encryption(plaintext))
```
该实现只考虑了字母和数字,对于其他字符需要进行额外处理。