本帖最后由 jackz007 于 2025-10-22 13:18 编辑
- x = input("请输入明文:")
- y = input("请输入的位数:")
- a = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
- "O", "P", "Q", "R", "S", "T", "U", "W", "X", "Y", "Z"]
- b = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
- "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
- "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
- "....-", ".....", "-....", "--...", "---..", "----.", "-----"]
- result = []
- for e in x :
- c = a . index(e)
- result . append(b[(c + int(y)) % len(a)]) # 如果 c + int(y) > 25,b[c+int(y)] 会下标越界
- print(result)
复制代码
【运行实况】:
- D:\[00.Exercise]\[Python]>python mos.py
- 请输入明文:LLLLLLLL
- 请输入的位数:2
- ['-.', '-.', '-.', '-.', '-.', '-.', '-.', '-.']
- D:\[00.Exercise]\[Python]>python mos.py
- 请输入明文:ZZZZZZZZ
- 请输入的位数:2
- ['-...', '-...', '-...', '-...', '-...', '-...', '-...', '-...']
- D:\[00.Exercise]\[Python]>
复制代码 |