在我的代码中遇到“列表索引超出范围”错误的问题

时间:2016-07-17 02:17:17

标签: python

关于这个任务的一些上下文,我得到了,我基本上必须创建一个程序,检查用户给出的答案,并说明他们是否正确,以及哪些错误作为输出。我大部分时间都解决了我的问题在于如何将“用户”答案存储在文本文件或其他内容中。使用eclipse,它说我print(str(index + 1) + "\t" + correct_list[index]+ "\t" + use_ans_1[index], end= "\t" )部分错了,并告诉我,我的列表索引超出了范围错误。

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()

2 个答案:

答案 0 :(得分:2)

我发现了一些问题并在下面解决了所有问题:

  1. 写完文件后你还没有关闭文件。
  2. 你在一行上写下了所有的答案,但后来试着把它们看作是分开的一行。
  3. 您没有从文件中读取的行上删除换行符。 (答案会像&#34; A \ n&#34;而不是&#34; A&#34;回来,所以它们看起来都不正确,会打印得很糟糕。)
  4. 您的if / else没有正确缩进,因此不属于while循环。
  5. 还要注意使用文件的with模式......这样就无法忘记关闭它们,因此我强烈建议您这样做。

    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()
    

答案 1 :(得分:1)

我已经修复了你的代码(但是看起来似乎也打败了我)

以下是我发现你做错了:

  • 未关闭所有文件。

  • 未正确阅读您的数据。 (IndexOutOfRange错误的原因)

  • while循环中的缩进不良。

这是您的固定代码

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)

    infile.write(straw)
    infile.close()

    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()[0].split(", ")
    infile.close()

    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")

main()

希望这会有所帮助。 随意提出任何问题。