IndexError occure if you access a list beyond its content:
a = [1,2,3]
print(a[99]) # IndexError, has only indexes 0,1,2
You can catch the error:
try:
print(a[99])
except IndexError:
print("Item doesnot exist") # this is printed
or check your list first:
if len(a)>=100:
print(a[99]) # would avoid the error
Reading CSV often gets this kind of error if data is not of equal lenght or if you read the line after the last \n and it is empty - and you split/access it non the less.
You might want to restructure your code a bit and use namedtuples for more clarity:
Create the data:
q = "questions.txt"
with open(q,"w") as f:
f.write("""Which birthstone is associated with the month of May?,Diamond,Ruby,Emerald,Sapphire,
C
Which two colours as on the flag of Poland?,Red and Green, Blue and White, Green and White, Red and White,
D
""") # your problem is probably here, line is read and split and accessed on [0] etc.
# it has no data in it -> IndexError
The quiz-code:
from collections import namedtuple
QuizRecord = namedtuple('Quiz', 'question,a1,a2,a3,a4,solution')
# this creates a namedtuple with fields for
# question
# a(nswer)1 a(nswer)2 a(nswer)3 a(nswer)4
# solution
Q = []
pos = {"A":1, "B":2, "C":3, "D":4} # map solution letter to position in parts,
# 0 would be the question
with open(q) as f:
for line in f:
parts=line.strip("\n,").split(",")
if not parts:
print("Done reading lines")
break # done reading
# get the next line and get the correct solution from parsed parts
sol = pos.get(next(f).strip("\n,"),-1)
if sol == -1:
print("Done reading lines")
break # done reading
# add a new namedtuple to our quizzes
parts.append(parts[sol]) # add solution as text to ease comparisons
Q.append(QuizRecord._make(parts)) # add a new namedtuple to Q using parts
for question, a1, a2, a3, a4, sol in Q:
print(question)
print("Solutions: ", ' '.join( (a1,a2,a3,a4) ))
ans = input("Input your answer: ").lower()
if ans == sol.lower():
print("Correct!\n")
else:
print(f"Nope, the answer is {sol}\n")
Output:
Which birthstone is associated with the month of May?
Solutions: Diamond Ruby Emerald Sapphire
Input your answerEmerald
Correct!
Which two colours as on the flag of Poland?
Solutions: Red and Green Blue and White Green and White Red and White
Input your answerRed and Green
Nope, the answer is Red and White
Documentation: