In this excercise
https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-lists/projects/python-gradebook
we have to add/modify one value in the last list item.
As i understand the task correctly we have to put 98 value to the last list item.
It works fine when one types
gradebook[-1] = 98
it’s also works fine when you type
gradebook[7] = 98
Why is that? The last list item should be # 5 (as we count from 0) not 7.
And why we can’t modify it using
gradebook[5][1] = 98? Which looks correct.
Step 7?
gradebook[7] = 98
doesn’t work. You get a “list assignment index out of range” error.
You have to access both indicies in the two dimensional list to change the score to 98. So, gradebook[-1] = 98
also wouldn’t work.
gradebook[5][1] = 98
works as does
gradebook[-1][-1] = 98
Somehow they work in the excercise.I wouldn’t post these lines of code if they didn’t work.
Here’s a screen where gradebook’s value is -1
here’s another screen where gradebook’s value is 7
As you can see, both codes work fine, there’s no error.
Hi aggy_m,
Setting gradebook[7] = 98
works in your code because gradebook
has a length of 8
on line 11.
When you originally create gradebook
on line 6, it has a length of 4
.
After that, each time you call gradebook.append()
on lines 7-10 the length of gradebook
increases by 1
.
Try printing gradebook
with print(gradebook)
just before line 11. You can also print its length with print(len(gradebook))
.
You can see in lisalisaj’s code gradebook
only has a length of 5
when they tried to assign gradebook[7] = 98
, so they get an IndexError
.
They don’t work. “computer science” and “visual arts” are within the same brackets…