Index error line 13 on my code. list index is out of range

Viewed 45

I'm a newbie at python, trying to write a code about the ABC quiz, a tutorial on YT, but when I run it shows an error. line 13, in Question(question_prompts[1], "a"), IndexError: list index out of range

here are the code


question_prompts = [
"How many time does Sebastian Vettel win a World Driver Championship on F1?\n(a). never\n(b). 2x\n(c). 5x\n(d). 4x"
"When was the last time Michael Schumacher won f1 WDC?\n(a). year 2004\n(b). year 2011\n(c). year 2006\n(d). year 2005"
"Track that held an F1 Grand Prix more than any circuits\n(a). Monza\n(b). Silverstone\n(c). Monaco\n(d). Spa"
"What was the opening Grand Prix of the season before Australia become the season opener?\n(a.)Bahrain\n(b). China\n(c). Abu Dhabi\n(d). Spain"
"Who is the only driver that had 5 WDC until now?\n(a). Lewis Hamilton\n(b). Alain Prost\n(c). Juan Manuel Fangio\n(d). Aryton Senna"
]

questions = [
    Question(question_prompts[0], "d"),
    Question(question_prompts[1], "a"),
    Question(question_prompts[2], "a"),
    Question(question_prompts[3], "a"),
    Question(question_prompts[4], "c"),
]

def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question_prompts)
        if answer == question.answer:
            score += 1

            print("Hey you got " + str(score) + " / " + str(len(questions)) + " Correct")
1 Answers

Assuming you wanted your question_prompts to be a list with 5 elements, then you need commas at the end of each line, like so:

question_prompts = [
    "How many time does Sebastian Vettel win a World Driver Championship on F1?\n(a). never\n(b). 2x\n(c). 5x\n(d). 4x",
    "When was the last time Michael Schumacher won f1 WDC?\n(a). year 2004\n(b). year 2011\n(c). year 2006\n(d). year 2005",
    "Track that held an F1 Grand Prix more than any circuits\n(a). Monza\n(b). Silverstone\n(c). Monaco\n(d). Spa",
    "What was the opening Grand Prix of the season before Australia become the season opener?\n(a.)Bahrain\n(b). China\n(c). Abu Dhabi\n(d). Spain",
    "Who is the only driver that had 5 WDC until now?\n(a). Lewis Hamilton\n(b). Alain Prost\n(c). Juan Manuel Fangio\n(d). Aryton Senna",
]

Python has implicit concatenation of string literals, so: "a""b" == "ab".

Because of this implicit concatenation, what you have currently is a list with a single element, like this:

question_prompts = [
    "How many time does Sebastian Vettel win a World Driver Championship on F1?\n(a). never\n(b). 2x\n(c). 5x\n(d). 4xWhen was the last time Michael Schumacher won f1 WDC?\n(a). year 2004\n(b). year 2011\n(c). year 2006\n(d). year 2005Track that held an F1 Grand Prix more than any circuits\n(a). Monza\n(b). Silverstone\n(c). Monaco\n(d). SpaWhat was the opening Grand Prix of the season before Australia become the season opener?\n(a.)Bahrain\n(b). China\n(c). Abu Dhabi\n(d). SpainWho is the only driver that had 5 WDC until now?\n(a). Lewis Hamilton\n(b). Alain Prost\n(c). Juan Manuel Fangio\n(d). Aryton Senna"
]

Obviously not your intention :)

Related