Vigenere python - 找不到子字符串错误

Posted

技术标签:

【中文标题】Vigenere python - 找不到子字符串错误【英文标题】:Vigenere python - substring not found error 【发布时间】:2015-12-07 18:01:52 【问题描述】:

这段代码一直工作到第 31 行。然后错误 ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintext) ValueError: substring not found 出现。 我不确定为什么会这样,你能帮我吗? 我正在尝试使用此代码中的关键字加密代码。我可以用数字加密 [如开头所示],但我现在想使用关键字来创建“Vigenere 密码”。

这是完整的代码:

eord = input("Would you like to encrypt or decrypt? [e/d]: ")
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012346789"
string = ''

if eord == 'e':
    texte = input ("Type your word to encrypt: ")
    key1 = int(input("Choose a key: "))
    for letter in texte:
        number = (ord(letter)) + (key1)
        letter = (chr(number))
        string = (str(string)) + (str(letter))
    print (string)
    keyword = input ("Would you like to encrypt your code further? [Y/N]: ")



    if keyword == "Y" or "y":
        plaintext = input("Please enter plain text: ")
        key = input("Please enter a keyword: ")
        keylist = []
        keylength = 0
        while keylength < len(plaintext):
            for char in  key:
                if keylength < len(plaintext):
                    keylist.append(str(char))
                    keylength = keylength + 1
        completeciphertext = []
        ciphercharindexvalue = 0
        keyincrement = 0
        for plaintextchar in plaintext:
            ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintext)
            while ciphercharindexvalue >25:
                ciphercharindexvalue = ciphercharindexvalue - 26
            completeciphertext.append(alphabet[ciphercharindexvalue])
            keyincrement = keyincrement + 1
        print (''.join(completecipertext))

【问题讨论】:

【参考方案1】:

您正在尝试获取纯文本而不是纯文本字符的索引。也许这会起作用:

ciphercharindexvalue = alphabet.index(keylist[keyincrement]) + alphabet.index(plaintextchar)

【讨论】:

以上是关于Vigenere python - 找不到子字符串错误的主要内容,如果未能解决你的问题,请参考以下文章

python中的re.search()函数找不到子字符串

ValueError:在 midi 中找不到子字符串

UndefinedBehaviorSanitiser 问题 - CS50 Vigenere

Python 子进程调用返回“找不到命令”,终端正确执行

String.includes 找不到子字符串

Python Vigenere 工作,但我无法使用函数来解释空格和非字母字符