试图在python中编写一个cypher程序,我就在那里

时间:2013-10-16 04:03:35

标签: python encryption

到目前为止,这是我的代码。

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"

老实说,我想不出办法做到这一点。任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:1)

我想这个练习的目的是教你切片时的“步幅”(又称步骤)选项。

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。我会把剩下的留给你。

答案 1 :(得分:0)

>>> 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

我认为会起作用

答案 2 :(得分:-1)

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"

您无需导入任何软件包,只需简单。