Codecademy Forums

Python: classes bonus challenges: Student.attendance()

Hi there,
Can anyone help. I’ve been battling with the .attendance bonus challenge in the classes section of the python path. Where we add a student’s attendance to a dictionary of dates and booleans.

I keep getting the error: “TypeError: ‘dict’ object is not callable” It’s fine the first time it’s used but whenever I try to add a second entry into the dictionary I get the error message. Here’s my code below.

def attendance (self, boolean):
import datetime
date = datetime.date.today()
self.attendance = {}
return self.attendance.update({date: boolean})

Thanks

Normally we import modules at the top of the code, not inside a function.

attendance() is an instance method and attempting to define a variable of the same name could be the problem, here.

Please post a link to the exercise so we can examine the instructions and test some examples.

Great thanks. Here;s the link to the exercise: https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/data-types/exercises/review

The question is:
“Add an instance variable to Student that is a dictionary called .attendance , with dates as keys and booleans as values that indicate whether the student attended school that day.”

1 Like

The instance variable should be defined in the __init__() method.

As for how it is updated that will take another method.

Eg.

# before the class
from datetime import datetime

# in the class
def set_attendance(self, value):
    now = datetime.now()
    self['{}/{}/{}'.format(now.year, now.month, now.day)] = value
1 Like