 # I need help to solve these two problems

• Write a `Student` method `get_average()` that returns the student’s average score.
• 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.

Anyone could help me solving these 2 problems??

1 Like

A `Grade` object has a `score` attribute that we must refer to in order to access the grade value. That means as we iterate over the `grades` attribute value list, we need to extract the score from the object.

``````scores = []
``````

Now with that list, we can calculate the sum and dividing by the length, the average.

``````average = sum(scores) / len(scores)
``````

If you’re not familiar with the `sum()` function, use the naive approach to accumulate a sum.

``````total = 0
for score in scores:
total += score
average = total / len(scores)
``````

The attendance attribute could take a number of approaches. Let’s pretend this is a daily process that draws on the `datetime` module so dates are consistent and machine generated.

The Student attribute will be,

``````self.attendance = {}
``````

There are two methods that may come into play. One method that runs everyday, and one that can be used to update the data.

1 Like

A start would be to identify what’s stopping you from doing this, so that you can start looking up how to do those things and/or try them out separately before adding them to your current code

1 Like

But at the code below, I just couldn’t get what grade.score refers to and why to use it:

``````scores = [ ]
``````

I also didn’t get the second part of the answer which is about adding an instance variable to `Student` (class) that is a dictionary called `.attendance`.

Would you please explain those in more detail?

1 Like

An instance of the Grade class will have one attribute, `self.score`. Every grade that is added to our Student instance is an instance of the Grade class.

``````self.grades = []
``````

We start with an empty list, then use the `add_grade` method to insert new instances of Grade into the list. We cannot access the list directly because all it will show us is the lot of Grade instances as type=Grade.

When we iterate over `self.grades` we get one of those objects at a time. To access the score attribute we poll the instance.score value.

``````for grade in self.grades:
total += grade.score    # this will be a number
``````

We add new code to the class definition so that all instances have that attribute. I’m not sure that we can do this dynamically while other instances exist already since they may not be updated (needs testing and confirmation) so just add the code directly to the class.

``````class Student:
def __init__(self, ..., ...):
self. ... = ...
self. ... = ...
self.attendance = {}
``````

`...` means fill in the blanks.

We will need a method to update the dictionary, which will have a key/value pair:

``````self.attendance[today] = bool
``````

where `bool` is the boolean we set, either `True` or `False`. We can enter the date manually, but for consistency we can draw the date from the `datetime.now()` object.

``````>>> today = '{:<4d}-{:>02d}-{:>02d}'.format(now.year, now.month, now.day)
>>> today
'2019-01-15'
>>>
``````
1 Like

Thank you very much!

1 Like