-1

I'm writing a vigenere cipher for a personal project and i am coming across a index error. it is saying IndexError: list index out of range. The line causing this is IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar).

Here is all of the code:

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

The section it is happening is this:

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

What is wrong with the code because i tried using pycharm and it highlighted [keyIncrement] but i don't know how to fix this. Thanks for any help in advance.

  • Read the error message more carefully. You haven't included any of the useful information such as the line number etc. All the information you need is there. The most likely issue is that `keyIncrement` is larger than the size of the list `keyList`. Try printing the index and the list size right before the line. – Martin Konecny Dec 13 '15 at 19:50
  • I would try debugging it with PyCharm and putting a breakpoint on the line with the error. That should give you an idea of which value is out of range and help you figure out what set it to an out of range value. – Scotty Waggoner Dec 13 '15 at 19:52
  • 'Traceback (most recent call last): File "/home/owain/Documents/USB 2/##Python Cipher##.py", line 33, in IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar) IndexError: list index out of range' . This is the error message – Owain Williams Dec 13 '15 at 19:59
  • 1
    School project? Someone came here with the same [code](http://stackoverflow.com/questions/34246133/how-do-i-force-my-code-to-print-in-python/34246163#34246163) yesterday. Looks exactly the same. The error is also in the same part of the code. Check there for an answer. –  Dec 13 '15 at 20:01
  • Sorry i dont know how to use pycharm very well. know and good tutorials? – Owain Williams Dec 13 '15 at 20:02
  • Im the same person but couldn't get the answer. And i made a new acount as i had some bad posts. – Owain Williams Dec 13 '15 at 20:08
  • Oh I thought you said the other persons answer helped so I stopped trying to figure it out –  Dec 13 '15 at 20:10
  • Oh i see. It did help but a new error was created, list index out of range. – Owain Williams Dec 13 '15 at 20:12
  • Can you send me the exact error message please –  Dec 13 '15 at 20:13
  • 'Traceback (most recent call last): File "/home/owain/Documents/USB 2/##Python Cipher##.py", line 33, in IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar) IndexError: list index out of range' – Owain Williams Dec 13 '15 at 20:15
  • OK, the exact part that is causing troubles it the little function here `Alphabet.index(keyList[keyIncrement])` –  Dec 13 '15 at 20:22
  • Ok, so how can i fix this? – Owain Williams Dec 13 '15 at 20:30
  • OK, so `keyList` only has 1 value which is 'c'. And that would be in position 0 of the list. `keyIncrement = 1`, so your are calling for a value in place 1 of `keylist` which doesn't exist. –  Dec 13 '15 at 20:33
  • Could you write it out in the section of the code im finding it difficult to understand. Thanks. – Owain Williams Dec 13 '15 at 20:34
  • Fixed it! :) back to studying for my mid-terms –  Dec 13 '15 at 20:57
  • @OwainWilliams ok found the problem again this error pops up when the key is shorter than plain text. Would you want to continue to loop the characters in the key until it's finished?? –  Dec 13 '15 at 21:49
  • So the text is split up? – Owain Williams Dec 13 '15 at 21:55
  • no if so i would like it to be together – Owain Williams Dec 13 '15 at 22:10

1 Answers1

0

Your problem was this if keyLength < len(plainText): statement blocking the for character in key from adding each character to list before running the rest of the code.

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

Test run:

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 sn snx snxm Traceback (most recent call last): File "/home/owain/Documents/USB 2/##Python Cipher##.py", line 32, in IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar) IndexError: list index out of range – Owain Williams Dec 13 '15 at 21:09
  • really I just ran through it a few times what are you typing is it still `e`, `hello`, `3`, `encrypt`, `python`, `cypher`? –  Dec 13 '15 at 21:13
  • Ok. i just tryed what you typed in and that worked however when i typed in e hello 3 encrypt darrien brit it didn't. – Owain Williams Dec 13 '15 at 21:21
  • Can you do me a huge favor please? Edit your answer with comments using `#` after every line of code in the problem area (entire part in my answer), so I can see why you put each part this will save me time. –  Dec 13 '15 at 21:28
  • I'm starting mid-terms in a few days, so trying to help you while studying –  Dec 13 '15 at 21:33
  • @OwainWilliams ^^^^^^^^^^^^^^^^^^^^ –  Dec 13 '15 at 21:38
  • I dont know how to explain it sorry – Owain Williams Dec 13 '15 at 21:49
  • @OwainWilliams And finally I found the final answer –  Dec 13 '15 at 22:11
  • @OwainWilliams Also, is it cool if I used your algorithm for my password keeper program? –  Dec 13 '15 at 22:12
  • yea sure. you helped alot – Owain Williams Dec 13 '15 at 22:14