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

Vigenere python - substring not found error

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

这是完整代码:

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

您正在尝试获取明文而非明文字符的索引。也许这会奏效:

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