Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带空格句子的Vigenere密码_Python_Cryptography_Vigenere - Fatal编程技术网

Python 带空格句子的Vigenere密码

Python 带空格句子的Vigenere密码,python,cryptography,vigenere,Python,Cryptography,Vigenere,我想为句子制作一个维格纳密码程序。 例如,消息是“介绍我最好的朋友卡莉”,关键字是“攀登” 输出是 Introducing my friend carly climbclimbclimbclimbclimbcl 而我想要的输出是 Introducing my friend carly climbclimbc li mbclim bclim 如何做到这一点?一种方法是使用itertools并在原始消息上循环: import itertools y = "Introducing my

我想为句子制作一个维格纳密码程序。 例如,消息是“介绍我最好的朋友卡莉”,关键字是“攀登”

输出是

Introducing my friend carly
climbclimbclimbclimbclimbcl
而我想要的输出是

Introducing my friend carly
climbclimbc li mbclim bclim

如何做到这一点?

一种方法是使用itertools并在原始消息上循环:

import itertools
y = "Introducing my best friend carly"
target_length = len(y)
a_string = "climb"


def repeat_string(a_string,y):
    repeated_word = itertools.cycle(a_string)
    return "".join([next(repeated_word) if letter.isalpha() else letter for letter in y])



print (y)
print(repeat_string(a_string,y))
输出:

Introducing my best friend carly
climbclimbc li mbcl imbcli mbcli
Introducing, my best friend carly.
climbclimbc, li mbcl imbcli mbcli.
编辑:这应该保留任何标点符号, 输入:

输出:

Introducing my best friend carly
climbclimbc li mbcl imbcli mbcli
Introducing, my best friend carly.
climbclimbc, li mbcl imbcli mbcli.

非常感谢!如果我想再添加一个条件,如果有标点符号,如何保留标点符号?我在string.标点符号中添加了elif字母,但出现错误。@c.jungc,我添加了答案。它保留标点符号。不需要elif语句。或者我误解了你,你想解码标点符号吗?对不起,我弄错了,我弄错了。你的答案已经保留了标点符号