'Type error: list indices must be integers or slices, not tuple or Type error: 'list' and 'int'
I have checked similar questions and attempted various techniques and nothing works. I have included two versions of my code. I need to calculate and store the total mark for each student by going through each row in the array Student Mark but it won't run due to a type error "TypeError: list indices must be integers or slices, not tuple"
I originally thought it was the commas, which have been the cause of most of the solutions I checked, I have double-checked these all. I then read about the range problem and attempted to address this in version 2, still no luck!
If you can help that would be very beneficial, I am new with this but can normally at least debug it...now I'm stuck though.
Version 1:
SubjectNo = 5
StudentName = ["Bob", "Jimmy", "Michael", "Jennifer", "Linda", "Stella", "Steven", "Lisa", "Sarah", "Jessica"]
StudentMark = [
[76, 38, 80, 54, 55, 69, 91, 100, 64, 71, 49],
[90, 44, 99, 19, 78, 36, 51, 53, 72, 86, 43],
[48, 79, 59, 40, 49, 88, 46, 92, 50, 21, 36],
[86, 54, 38, 63, 52, 82, 67, 44, 100, 98, 60],
[66, 88, 79, 70, 22, 62, 33, 88, 91, 75, 43]
]
TotalMark = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
AvergaeMark = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
GradeAwarded = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
for i in range(0,ClassSize):
for m in range(0,SubjectNo):
TotalMark[i] = TotalMark[i] + StudentMark[i,m]
for i in range(0,9):
AverageMark[i] = TotalMark[i]/10
for i in range(0,9):
if AvergaeMark[i] >= 70:
GradeAwarded[i]= "distinction"
if AverageMark[i] >=55 and AverageMark[i]< 70:
GradeAwarded[i] = "merit"
if AverageMark[i] >=40 and AverageMark[i]<55:
GradeAwarded[i] = "pass"
if AverageMark[i] < 40:
GradeAwarded[i] = "fail"
for i in range(0,9):
print(StudentName[i], TotalMark[i], AverageMark[i], GradeAwarded[I])
Version 2:
ClassSize, SubjectNo = (10, 5)
StudentName = ["Bob", "Jimmy", "Michael", "Jennifer", "Linda", "Stella", "Steven", "Lisa", "Sarah", "Jessica"]
StudentMark = [
[76, 38, 80, 54, 55, 69, 91, 100, 64, 71, 49],
[90, 44, 99, 19, 78, 36, 51, 53, 72, 86, 43],
[48, 79, 59, 40, 49, 88, 46, 92, 50, 21, 36],
[86, 54, 38, 63, 52, 82, 67, 44, 100, 98, 60],
[66, 88, 79, 70, 22, 62, 33, 88, 91, 75, 43]
]
TotalMark = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
AvergaeMark = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
GradeAwarded = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
for i in range(ClassSize):
for row in StudentMark:
TotalMark[i] += TotalMark[i] + StudentMark[i, row]
for i in range(0,9):
AverageMark[i] = TotalMark[i]/10
for i in range(0,9):
if AvergaeMark[i] >= 70:
GradeAwarded[i]= "distinction"
if AverageMark[i] >=55 and AverageMark[i]< 70:
GradeAwarded[i] = "merit"
if AverageMark[i] >=40 and AverageMark[i]<55:
GradeAwarded[i] = "pass"
if AverageMark[i] < 40:
GradeAwarded[i] = "fail"
for i in range(0,9):
print(StudentName[i], TotalMark[i], AverageMark[i], GradeAwarded[i])
Neither work and create the same issue.
Solution 1:[1]
This should work:
ClassSize, SubjectNo = (10, 5)
StudentName = ["Bob", "Jimmy", "Michael", "Jennifer", "Linda", "Stella", "Steven", "Lisa", "Sarah", "Jessica"]
StudentMark = [
[76, 38, 80, 54, 55, 69, 91, 100, 64, 71, 49],
[90, 44, 99, 19, 78, 36, 51, 53, 72, 86, 43],
[48, 79, 59, 40, 49, 88, 46, 92, 50, 21, 36],
[86, 54, 38, 63, 52, 82, 67, 44, 100, 98, 60],
[66, 88, 79, 70, 22, 62, 33, 88, 91, 75, 43]
]
TotalMark = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
AverageMark = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
GradeAwarded = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
for i in range(SubjectNo):
for row in range(len(StudentMark)):
TotalMark[i] += TotalMark[i] + StudentMark[i][row]
for i in range(0,10):
AverageMark[i] = TotalMark[i]/10
for i in range(0,10):
if AverageMark[i] >= 70:
GradeAwarded[i]= "distinction"
if AverageMark[i] >=55 and AverageMark[i]< 70:
GradeAwarded[i] = "merit"
if AverageMark[i] >=40 and AverageMark[i]<55:
GradeAwarded[i] = "pass"
if AverageMark[i] < 40:
GradeAwarded[i] = "fail"
for i in range(0,10):
print(StudentName[i], TotalMark[i], AverageMark[i], GradeAwarded[i])
Output:
Bob 2003 200.3 distinction
Jimmy 2304 230.4 distinction
Michael 1765 176.5 distinction
Jennifer 2138 213.8 distinction
Linda 2238 223.8 distinction
Stella 0 0.0 fail
Steven 0 0.0 fail
Lisa 0 0.0 fail
Sarah 0 0.0 fail
Jessica 0 0.0 fail
There are a several reasons why your code did not work. First, it does not work in python to call a list like list[someint, otherint]. In your case that was StudentMark[i, row] which should be StudentMark[i][row]. Second your ClassSize is 10 but it should be 5 as there are only five items in the StudentMark. Also, you had AvergaeMark instead of AverageMark but you used both of them in the program so I just switched it to AverageMark
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | catasaurus |
