我是python初学者,尝试构建一个简单的密码,在该密码中我遍历一个列表,然后加3以构建一个新列表,但始终收到类型错误。lower_list =来自abcd的列表..... z
当我执行以下命令时,出现类型错误:
for i in lower_list:
shift_lower += lower_list[i:i+3]
任何人都提供有关如何在语法上正确地完成此操作的提示?谢谢。
你试试这个。
lower_list=['a','b',...,'z']
cipher_text=[chr(ord(i)+3) for i in lower_list]
['d',
'e',
'f',
...
'z',
'{',
'|',
'}']
编辑:
当字符的域和范围为a-z
(为简洁起见,我考虑小写字母az)。这是Caeser Cipher的示例。
C.T=(P.T+K)Mod26
实现方式:
lower_list=['a', 'b' ,'c', ..., 'z']
cipher_text=[chr((ord(s) + incr - 97) % 26 + 97) for s in lst]
您可以构建一个函数来处理加密和解密。我会这样。
def caeser_cipher(lst,incr,encrypt=True):
if encrypt:
return [chr((ord(s) + incr - 97) % 26 + 97) for s in lst]
else:
return [chr((ord(s) - incr - 97) % 26 + 97) for s in lst]
lower_letters=['a','b', ...'z']
cipher_text=caeser_cipher(lower_letters,4)
#['e', 'f', 'g', 'h', ... ,'c', 'd']
plain_text=caeser_cipher(cipher_text,4,encrypt=False)
# ['a', 'b', 'c', ...,'z']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句