【问题标题】:IndexError: list index out of range error in pythonIndexError:python中的列表索引超出范围错误
【发布时间】:2015-12-13 19:40:41
【问题描述】:

我正在为个人项目编写 vigenere 密码,但遇到了索引错误。它说IndexError: list index out of range。 造成这种情况的行是IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)

所有代码如下:

playing = True
string = ""
Alphabet = ('z','a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')

while playing == True:
    string = ""
    eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')

    if eord == 'e':
        texte = input ("Type your word to encrypt: ")
        key1 = int(input("Choose a key between 1-26: "))
        for letter in texte:
            number = (ord(letter)) + (key1)
            letter=(chr(number))
            string = (str(string)) + (str(letter))
        print (string)
        keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")

        if keyword == 'encrypt':
            plainText = input("Please enter the plain text: ")
            key = input("Please enter the key: ")
            keyList = []
            keyLength = 0
            while keyLength < len(plainText):
                for char in key:
                     if keyLength < len(plainText):
                         keyList.append(str(char))
                         keyLength = keyLength + 1
                         CipherText = [] 
                         IndexValue = 0
                         keyIncrement = 0
                     for plainTextChar in plainText:
                         IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
                         while IndexValue > 26:
                             IndexValue = IndexValue - 26
                         CipherText.append(Alphabet[IndexValue])
                         keyIncrement = keyIncrement + 1
                         print (''.join(CipherText))

            finish = input('Would you like to go again Y or N')
            if finish == 'n' or finish == 'N':
                retry = input ("Would you like to go again? Y or N: ")
                if retry == 'N' or retry == 'n':
                    print ("Please exit the window")
                    import time
                    time.sleep(1)
                    import sys
                    sys.exit()

    elif eord == 'd':
        texd = input ("Type your word to decrypt: ")
        key2 = int(input("Choose a key between 1-16: "))

        for letter in texd:
            number = (ord(letter)) - (key2)
            letter=(chr(number))
            string = (str(string)) + (str(letter))
        print (string)
        keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")

        if keyword == 'decrypt':
             plainText = input("Please enter the plain text: ")
             key = input("Please enter the key: ")
             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(plainTextChar)
                         while cipherCharIndexValue > 26:
                             cipherCharIndexValue = cipherCharIndexValue + 26
                         completeCipherText.append(Alphabet[cipherCharIndexValue])
                         keyIncrement = keyIncrement - 1
                         print (''.join(completeCipherText))

                         finish = input('Would you like to go again Y or N')
                         if finish == 'n' or finish == 'N':
                             retry = input ("Would you like to go again? Y or N: ")
                             if retry == 'N' or retry == 'n':
                                 print ("Please exit the window")
                                 import time
                                 time.sleep(1)
                                 import sys
                                 sys.exit()

正在发生的部分是这样的:

if keyword == 'encrypt':
            plainText = input("Please enter the plain text: ")
            key = input("Please enter the key: ")
            keyList = []
            keyLength = 0
            while keyLength < len(plainText):
                for char in key:
                     if keyLength < len(plainText):
                         keyList.append(str(char))
                         keyLength = keyLength + 1
                         CipherText = [] 
                         IndexValue = 0
                         keyIncrement = 0
                     for plainTextChar in plainText:
                         IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
                         while IndexValue > 26:
                             IndexValue = IndexValue - 26
                         CipherText.append(Alphabet[IndexValue])
                         keyIncrement = keyIncrement + 1
                         print (''.join(CipherText))

代码有什么问题,因为我尝试使用 pycharm,它突出显示了 [keyIncrement],但我不知道如何解决这个问题。感谢您提前提供任何帮助。

【问题讨论】:

  • 仔细阅读错误信息。您没有包含任何有用的信息,例如行号等。您需要的所有信息都在那里。最可能的问题是keyIncrement 大于列表keyList 的大小。尝试在行前打印索引和列表大小。
  • 我会尝试使用 PyCharm 对其进行调试,并在出现错误的行上放置一个断点。这应该让您了解哪个值超出了范围,并帮助您弄清楚是什么将其设置为超出范围的值。
  • 'Traceback(最近一次调用最后):文件“/home/owain/Documents/USB 2/##Python Cipher##.py”,第 33 行,在 IndexValue = Alphabet。 index(keyList[keyIncrement]) + Alphabet.index(plainTextChar) IndexError: list index out of range' 。这是错误信息
  • 学校项目?昨天有人带着同样的code来到这里。看起来完全一样。错误也在代码的同一部分。检查那里的答案。
  • 对不起,我不知道如何很好地使用pycharm。知道和好的教程?

标签: python encryption caesar-cipher vigenere


【解决方案1】:

您的问题是 if keyLength &lt; len(plainText): 语句阻止 for character in key 在运行其余代码之前将每个字符添加到列表中。

while keyLength < len(plainText):
    try:
        char = key[num]
    except IndexError:
        num = 0
        char = key[num]
    keyList.append(str(char))
    keyLength += 1
    num += 1
    CipherText = [] 
    IndexValue = 0
    keyIncrement = 0
for plainTextChar in plainText:
     IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
     while IndexValue > 26:
         IndexValue = IndexValue - 26
     CipherText.append(Alphabet[IndexValue])
     keyIncrement = keyIncrement + 1
     print (''.join(CipherText))

试运行:

Type "d" to "decrypt" and "e" to "encrypt": e
Type your word to encrypt: hello
Choose a key between 1-26: 3
khoor
Type 'encrypt' code further or 'decrypt' further: encrypt
Please enter the plain text: darrien
Please enter the key: brit
f
fs
fsa
fsal
fsalk
fsalkw
fsalkww
Would you like to go again Y or N

【讨论】:

  • s snx snxm Traceback(最近一次调用最后一次):文件“/home/owain/Documents/USB 2/##Python Cipher##.py”,第 32 行,在 IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar) IndexError: 列表索引超出范围
  • 真的,我只是跑了几次你在输入什么,它仍然是ehello3encryptpythoncypher?跨度>
  • 好的。我刚刚尝试了你输入的内容,但是当我输入 e hello 3 encrypt darrien brit 时它没有。
  • 你能帮我一个大忙吗?在问题区域中的每一行代码之后使用 # 使用 cmets 编辑您的答案(我的答案中的整个部分),所以我可以明白为什么你把每个部分都放在这将节省我的时间。
  • 过几天就要开始期中考试了,想在学习的时候帮到你
猜你喜欢
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2016-07-29
  • 2014-10-13
  • 1970-01-01
相关资源
最近更新 更多