【发布时间】:2016-01-17 02:16:45
【问题描述】:
如果我有两个字符串,例如:
plaintext = "hello"
key = "hi"
如何在不超出范围的情况下将字母(或空格和标点符号等其他字符)对齐?到目前为止,我正在这样做,但我一直遇到字符串超出索引错误。
encryption = ""
for index in range(len(plaintext)):
if plaintext[index] in alphabet:
encryption += vigenere_encrypt(plaintext[index], key[index])
if plaintext[index] not in alphabet:
encryption += plaintext[index]
return encryption
我实际上是在尝试使我的密钥与明文的长度相匹配,所以"hi" --> "hihih"
它与“hello”的长度相同,因此它可以同时循环遍历两者而不会遇到超出范围的错误
【问题讨论】:
-
您到底想达到什么目的?这两个字符串的预期输出是什么?
-
你为什么要建立索引以及 vigenere_encrypt 是什么?
-
我实际上是在尝试使我的密钥与纯文本的长度相匹配,因此“hi”应该变成“hihih”,它与“hello”的长度相同
-
我有一种感觉 str.translate 会做很多你的代码正在做的事情