索引错误第13行在我的代码.列表索引超出范围



我是一个python新手,试图编写一个关于ABC测试的代码,一个关于YT的教程,但是当我运行它时显示一个错误。

line 13, in <module>
Question(question_prompts[1], "a"),
IndexError: list index out of range

代码如下:


question_prompts = [
"How many time does Sebastian Vettel win a World Driver Championship on F1?n(a). nevern(b). 2xn(c). 5xn(d). 4x"
"When was the last time Michael Schumacher won f1 WDC?n(a). year 2004n(b). year 2011n(c). year 2006n(d). year 2005"
"Track that held an F1 Grand Prix more than any circuitsn(a). Monzan(b). Silverstonen(c). Monacon(d). Spa"
"What was the opening Grand Prix of the season before Australia become the season opener?n(a.)Bahrainn(b). Chinan(c). Abu Dhabin(d). Spain"
"Who is the only driver that had 5 WDC until now?n(a). Lewis Hamiltonn(b). Alain Prostn(c). Juan Manuel Fangion(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")

假设您希望question_prompts是一个包含5个元素的列表,那么您需要在每行末尾使用逗号,如下所示:

question_prompts = [
"How many time does Sebastian Vettel win a World Driver Championship on F1?n(a). nevern(b). 2xn(c). 5xn(d). 4x",
"When was the last time Michael Schumacher won f1 WDC?n(a). year 2004n(b). year 2011n(c). year 2006n(d). year 2005",
"Track that held an F1 Grand Prix more than any circuitsn(a). Monzan(b). Silverstonen(c). Monacon(d). Spa",
"What was the opening Grand Prix of the season before Australia become the season opener?n(a.)Bahrainn(b). Chinan(c). Abu Dhabin(d). Spain",
"Who is the only driver that had 5 WDC until now?n(a). Lewis Hamiltonn(b). Alain Prostn(c). Juan Manuel Fangion(d). Aryton Senna",
]

Python有字符串字面值的隐式连接,所以:"a""b" == "ab".

由于这种隐式连接,您当前拥有的是一个包含单个元素的列表,如下所示:

question_prompts = [
"How many time does Sebastian Vettel win a World Driver Championship on F1?n(a). nevern(b). 2xn(c). 5xn(d). 4xWhen was the last time Michael Schumacher won f1 WDC?n(a). year 2004n(b). year 2011n(c). year 2006n(d). year 2005Track that held an F1 Grand Prix more than any circuitsn(a). Monzan(b). Silverstonen(c). Monacon(d). SpaWhat was the opening Grand Prix of the season before Australia become the season opener?n(a.)Bahrainn(b). Chinan(c). Abu Dhabin(d). SpainWho is the only driver that had 5 WDC until now?n(a). Lewis Hamiltonn(b). Alain Prostn(c). Juan Manuel Fangion(d). Aryton Senna"
]

显然不是你的本意:)

最新更新