|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- x = input("请输入明文:")
- y = input("请输入的位数:")
- a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- b = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
- "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
- "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
- "....-", ".....", "-....", "--...", "---..", "----.", "-----"]
- x_code = list(x)
- result = []
- c = []
- for e in x_code:
- if e in a:
- c_old = a.index(e)
- c_new = (c + int(y)) % len(a)
- new_result = a[c_new]
- result.append(new_result)
- else:
- result.append(e)
- print(''.join(result))
复制代码
为什么?
错误:
请输入明文:LLL
请输入的位数:2
Traceback (most recent call last):
File "C:/Users/10690/Desktop/莫斯密码(进化).py", line 15, in <module>
c_new = (c + int(y)) % len(a)
TypeError: can only concatenate list (not "int") to list
原因非常明确:
- TypeError: can only concatenate list (not "int") to list
复制代码
意思是:鱼油试图把一个 list 和一个 int 相加。
问题出在这一行:
- c_new = (c + int(y)) % len(a)
复制代码
c 在程序中定义为:
也就是说它是一个 空列表。
但你在循环中其实是想对每个字母的索引(即整数 c_old)进行位移运算,而不是用列表 c。
正确写法:把 c 改成 c_old 就可以了:
- x = input("请输入明文:")
- y = input("请输入的位数:")
- a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- b = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
- "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
- "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
- "....-", ".....", "-....", "--...", "---..", "----.", "-----"]
- x_code = list(x)
- result = []
- for e in x_code:
- if e in a:
- c_old = a.index(e)
- c_new = (c_old + int(y)) % len(a)
- new_result = a[c_new]
- result.append(new_result)
- else:
- result.append(e)
- print(''.join(result))
复制代码
|
|