python 3 IndexError: list index out of range -
my problem average value on won't show returns error
traceback (most recent call last): (file location), line 29, in <module> average_score = [[x[8],x[0]] x in a_list] (file location), line 29, in <listcomp> average_score = [[x[8],x[0]] x in a_list] indexerror: list index out of range
the code
import csv class_a = open('class_a.txt') csv_a = csv.reader(class_a) a_list = [] row in csv_a: row[3] = int(row[3]) row[4] = int(row[4]) row[5] = int(row[5]) minimum = min(row[3:5]) row.append(minimum) maximum = max(row[3:5]) row.append(maximum) average = sum(row[3:5])//3 row.append(average) a_list.append(row[0:8]) print(row[8])
this works when test out values 0 7 ,even if change location of avarage sum still error
when call a_list.append(row[0:8])
you're appending array using indexes 0, 1, 2, 3, 4, 5, 6, , 7 row
. means when later iterate a_list
, x
variable has indexes 7, , you're trying access 8.
quick example:
>>> row = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> x = row[:8] >>> x[8] traceback (most recent call last): file "<stdin>", line 1, in <module> indexerror: list index out of range >>> x[7] 7 >>>
Comments
Post a Comment