I’m trying to do the Gradebook python project. For some reason I’m getting value error with specific index. Other index within the array I tried is working fine except for the specific index. Can anyone tell what’s wrong with the code :
JavaScript
x
34
34
1
last_semester_gradebook = [
2
["politics", 80],
3
["latin", 96],
4
["dance", 97],
5
["architecture", 65],
6
]
7
8
# list called subjects
9
subjects = ["physics", "calculus", "poetry", "history"]
10
11
# list called grades
12
grades = [98, 97, 85, 88]
13
14
# gradebooks
15
gradebook = [
16
[subjects[0], grades[0]],
17
[subjects[1], grades[1]],
18
[[subjects[2], grades[2]]],
19
[subjects[3], grades[3]],
20
]
21
22
print(gradebook)
23
24
# Adding additional grades
25
gradebook.append(["Computer science", 100])
26
gradebook.append(["Visual arts", 93])
27
28
# adding 5 points
29
gradebook[-1][-1] = (93 + 5)
30
print(gradebook)
31
32
gradebook.remove(['poetry', 85])
33
print(gradebook)
34
Other index position works except for 2nd index. Thank you in advance.
Advertisement
Answer
After reformatting your code, it becomes somewhat obvious that you’re doing something different with the second index:
JavaScript
1
7
1
gradebook = [
2
[subjects[0], grades[0]],
3
[subjects[1], grades[1]],
4
[[subjects[2], grades[2]]],
5
[subjects[3], grades[3]],
6
]
7
Presumably it should be:
JavaScript
1
7
1
gradebook = [
2
[subjects[0], grades[0]],
3
[subjects[1], grades[1]],
4
[subjects[2], grades[2]],
5
[subjects[3], grades[3]],
6
]
7