我正在尝试进行多次回答测试,但我不断提出“列表索引超出范围”的错误

时间:2019-07-28 18:09:08

标签: python-3.x

我正在尝试进行多项选择题测试,我想不出为什么/为什么我的问题之一超出范围。我是编码和观看视频的新手,比他有2个问题。 https://www.youtube.com/watch?v=SgQhwtIoQ7o

我试图解决一些问题/答案。

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


Traceback (most recent call last):
  File "C:/Users/hayes_kbosh1g/PycharmProjects/Giraffe/Ap.py", line 13, in <module>
    Question(question_promts[1], "b"),
IndexError: list index out of range

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

此代码将产生相同的错误:

question_prompts = [0]

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

错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-3d524ad6758c> in <module>
      7 questions = [
      8     Question(question_prompts[0], "a"),
----> 9     Question(question_prompts[1], "b"),
     10     Question(question_prompts[2], "b"),
     11     Question(question_prompts[3], "d"),

IndexError: list index out of range

此代码没有错误:

question_prompts = [0,1,2,3,4]

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