Python: Typeerror: 'float' Object Is Not Subscriptable
Solution 1:
The issue is that you're modifying the data
list while you're iterating over it, using data.insert
in calculate_grades
. This leads to the second iteration of the loop to see the grade
value from the previous iteration as line
, rather than the list of integers it is expecting.
I don't entirely understand what you're trying to do, so I can't suggest a solution directly. Perhaps you should make a separate list for the output, or modify line
in place, rather than inserting a new item into data
.
Solution 2:
The specific problem is because there are floats in data (eventually)
for line in data:
total_points = sum(line[1:6])
grade = get_grade(total_points)
data.insert(0,total_points)
data.insert(1,grade)
Because you insert it into your list, as 'grade'
The general problem is that you are modifying your list ('data') while you iterate over it, which is a bad idea - your logic will be hard to read at best, and easily loop forever.
Solution 3:
Uh, the issue is the part line[1:6]
in line 10. The variable line
is a float, so you can't take a sublist.
Post a Comment for "Python: Typeerror: 'float' Object Is Not Subscriptable"