Nitsuga Arashi Nitsuga Arashi - 4 months ago 7
Python Question

Having trouble with a 'list index out of range' error in my code

For some context on this assignment I was given, I basically have to create a program that checks the answers given by a user and says if they've gotten it correct or not and which ones they got wrong as an output. I've mostly solved it out to where my problem is in how I have the 'user's' answers stored in the text file or something else. Using eclipse, it says that I've the

print(str(index + 1) + "\t" + correct_list[index]+ "\t" + use_ans_1[index], end= "\t" )
part wrong and tells me that I've gotten a list index out of range error.

def main():

wrong_list = ["A", "C", "A", "A", "D", "B","C", "A", "C", "B", "A", "D","C", "C", "A", "B", "C", "A","C", "B"]
infile = open('user_answers.txt', 'w')
straw = ', '.join(wrong_list)
use_ans = infile.write(straw)

correct_list = ["A", "C", "A", "A", "D", "B","C", "A", "C", "B", "A", "D","C", "A", "D", "C", "B", "B","D", "A"]

use_ans_1 = []
correct_count = 0
incorrect = 0
num_questions = 20
index = 0

infile = open('user_answers.txt', 'r')

use_ans_1 = infile.readlines()

infile.close()

print("Q \t Correct \t Your \t Status")
print("# \t Answer \t Answer \n --------------------------")

while index < 20:

print(str(index + 1) + "\t" + correct_list[index]+ "\t" + use_ans_1[index], end= "\t" )

if use_ans_1[index] == correct_list[index]:

correct_count += 1
index += 1

print("Correct")

else:

incorrect += 1
index += 1

print("Wrong")

main()

Answer

I found a few issues and fixed them all below:

  1. You weren't closing the file after writing to it.
  2. You were writing all the answers on one line but then trying to read them as though they were on separate lines.
  3. You weren't stripping off the newline on the lines read from the file. (Answers would come back like "A\n" instead of "A", so they would all look incorrect and would print out badly.)
  4. Your if/else weren't indented properly and therefore weren't part of the while loop.

Also note the with pattern for using files... it makes it impossible to forget to close them, so I highly recommend doing that.

def main():

    wrong_list = ["A", "C", "A", "A", "D", "B","C", "A", "C", "B", "A", "D","C", "C", "A", "B", "C", "A","C", "B"]

    with open('user_answers.txt', 'w') as outfile:
        outfile.write('\n'.join(wrong_list))

    correct_list = ["A", "C", "A", "A", "D", "B","C", "A", "C", "B", "A", "D","C", "A", "D", "C", "B", "B","D", "A"]

    use_ans_1 = []
    correct_count = 0
    incorrect = 0
    num_questions = 20
    index = 0

    with open('user_answers.txt', 'r') as infile:
        use_ans_1 = [line.rstrip() for line in infile.readlines()]

    print("Q \t ocrr \t Your \t Status")
    print("# \t Answer \t Answer \n --------------------------")

    while index < 20:                 

        print(str(index + 1) + "\t" + correct_list[index]+ "\t" + use_ans_1[index], end= "\t" )

        if use_ans_1[index] == correct_list[index]:

            correct_count += 1
            index += 1

            print("Correct")

        else:

            incorrect += 1
            index += 1

            print("Wrong")

if __name__ == '__main__':
    main()
Comments