试图用python编写一个cypher程序,我已经半途而废了

2024-10-02 12:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我目前的密码。你知道吗

def encryptMessage():
msg = "I came, I saw, I conquered"
i = 0
numChar = len(msg)
while i < numChar:
  print msg[i:i+5]
  i=i+5

它返回这个

I cam
e, I 
saw, 
I con
quere
d

下一部分是让程序打印每行的第一个字母,然后打印第二个字母,然后打印第三个字母,依此类推。应该是这样的。你知道吗

"IesIqd ,a u c wce aI,or m ne"

我真的想不出一个办法。任何帮助都将不胜感激。你知道吗


Tags: 密码lendef字母msgconprintcam
3条回答
def encryptMessage():
    result = []
    msg = "I came, I saw, I conquered"
    result = "".join([msg[k::5] for k in range(5)])

您将得到输出-:

"IesIqd ,a u c wce aI,or m ne"

您不需要导入任何包,只需简单地导入即可。你知道吗

我想这个练习的目的是教你如何在切片时使用“跨步”(又名step)选项。你知道吗

msg = 'I came, I saw, I conquered'

msg[::5]
Out[22]: 'IesIqd'

msg[1::5]
Out[23]: ' ,a u'

More explanation of the syntax here。剩下的就交给你了。你知道吗

>>> from itertools import izip_longest
>>> ciphermap = izip_longest(*[msg[i:i+5] for i in range(0,len(msg),5)],fillvalue="")
>>> encoded = "".join(["".join(x) for x in ciphermap])
>>> print encoded
IesIqd ,a uc wceaI,orm  ne

我想会有用的

相关问题 更多 >

    热门问题