2015-12-13 24 views
-1

我正在为个人项目编写vigenere密码,并且遇到索引错误。它说的是IndexError: list index out of range。 造成这种情况的线路是IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)IndexError:python中的列表索引超出范围错误

这里是所有的代码:

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],但我不知道如何解决这个问题。感谢您提前提供任何帮助。

+0

更加仔细阅读错误消息。你没有包含任何有用的信息,例如行号等。你需要的所有信息都在那里。最可能的问题是'keyIncrement'大于'keyList'列表的大小。尝试在行之前打印索引和列表大小。 –

+0

我会尝试使用PyCharm进行调试,并在出错的位置放置一个断点。这应该让你知道哪个值超出范围,并帮助你找出将其设置为超出范围值的原因。 –

+0

'Traceback(last recent call last): File“/ home/owain/Documents/USB 2/## Python Cipher ##。py”,in line 33,in IndexValue = Alphabet.index(keyList [keyIncrement]) + Alphabet.index(plainTextChar) IndexError:列出索引超出范围'。这是错误消息 –

回答

0

你的问题是这个if keyLength < 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 
+0

小号 SN SNX snxm 回溯(最近通话最后一个):“/家/欧文/文档/ USB 2.0/## Python的密码## PY” 文件,第32行,在 IndexValue = Alphabet.index(keyList [keyIncrement])+ Alphabet.index(plainTextChar) IndexError:列表索引超出范围 –

+0

它做了一些但仍然有一个错误:( –

+0

真的我只是跑通过它几次你在键入什么,它仍然是'e','hello','3','encrypt','python','cypher'? –

相关问题