CS101 Python Gradebook Part 7

Hey all I am having trouble with the seventh step in the Project “Python Gradebook” in CS101. It is asking to amend a value by accessing and updating our two-dimensional list to change a grade value but I am getting the following error:

Traceback (most recent call last):
File “script.py”, line 10, in
gradebook[5][1] = 98
TypeError: ‘tuple’ object does not support item assignment

I know when we zipped the two lists together to make a two-dimensional list we’ve generated a tuple which is immutable but the instructions are hinting at accessing the value in the list using the following format: gradebook[-1]. Are we supposed to ignore that and find a workaround or is there something I am missing here?

Could you link the project and format any code- How do I format code in my posts??

As you said tuples are immutable. If you wanted a mutable sequence maybe you could create one using the tuple?

x = [1]
y = 2,  # creates a tuple
x[0] = 2  # assigning to objects in a lit is fine
y[0] = 2  # throws an error because the tuple is immutable
y = list(y)  # if we wanted a mutable sequence...

Hi! Thank you for the quick reply. I have linked the project and formatted/included my code below.

Project Link: https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-lists/projects/python-gradebook

last_semester_gradebook = [("politics", 80), ("latin", 96), ("dance", 97), ("architecture", 65)]

subjects = ['physics', 'calculus', 'poetry', 'history']

grades = [98, 97, 85, 88]

subjects.append('computer science')

subjects.append('visual arts')

grades.append(100)

grades.append(93)

gradebook = list(zip(subjects, grades))

gradebook[5][1] = 98

print(gradebook)

Hmm, seems like one of the paths that has been updated recently. The newer instructions don’t suggest zip at any point (or at least I didn’t see it) so perhaps you can work without it?

Part 4 suggests it should look like the following- [['physics', 98], ...

Have list comprehensions or for loops been introduced at this point? That might be a reasonable solution. Otherwise it does seem odd to create two lists in parts 1 and 2 and then be forced into then creating a lists of lists from these items.

If you don’t feel like typing it all out again the following would save you the trouble but the syntax might not be introduced for a little while-

List of lists from two lists

gradebook = [list(tup) for tup in zip(subjects, grades)]

2 Likes

This is has been immensely helpful. I was caught up in thinking zip was the only solution but after slowing down re-reading through Part 4 I’ve made it to this point and have a clear path ahead of me:

last_semester_gradebook = [("politics", 80), ("latin", 96), ("dance", 97), ("architecture", 65)]
#Create a list called subjects and fill it with the classes you are taking:
subjects = ['physics', 'calculus', 'poetry', 'history']
#Create a list called grades and fill it with your scores:
grades = [98, 97, 85, 88]
#Create a two-dimensional list to combine subjects and grades. Use the table below as a reference to associated values.
gradebook = [['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]

print(gradebook)

Thank you so much for your clarification and your time!

2 Likes