9/9 How is Everybody Doing? - A better understanding



After some trial and error I managed to finish the 'student becomes the teacher' lesson. Admittedly, I had to browse the forums for the last part, as some piece of code that was added in the beginning was deleted further down the lesson. See below for my code.

Today I went over the code again and found out that I don't yet fully understand how it all works. Mainly talking about how Python knows certain things and how functions and for loops work. I wrote down how I think it works, and it would be great if someone could give some feedback or point me in the right direction if I'm mistaken somewhere.

  • Am I correct that the function def average(numbers): doesn't do anything in this lesson?

  • In the def get_average(student): function 3 variables are made and added together. How does Python know what value's to pick? Is the average(student["homework"]) part referring to the previous created list in the dictionary? If so, how does Python know which list to take? Since there are multiple lists with the same name.

  • In the function def get_class_average(students): the for loop iterates over every student, executes the def get_average(student): function for each, and adds the result to the empty list.
    Is there here also a reference to the actual created list students (i.e students = [lloyd, alice, tyler])? And is that the parameter of the function, the students in the for loop, or perhaps both/none?
    I'm asking mainly because if you replace students in both print statements with [lloyd, alice, tyler], the code still works perfectly fine. Even though there is no reference to the students any more. Like so:

print get_class_average([lloyd, alice, tyler])
print get_letter_grade(get_class_average([lloyd, alice, tyler]))
  • When you create the list students = [lloyd, alice, tyler] it seems to me that it auto-refers to the names of the three dictionaries (if you look at the functions). Is this something Python does automatic?

Hope my questions are clear and someone can answer them for me. :slight_smile:


After redoing this course from scratch we see that the code we added in around the third lesson,

students = [ lloyd, alice, tyler ]

is indeed not carrying over with the rest of the code. Whatever is causing it to disappear may never be known (or fixed). There must be hundreds of mentions of this missing object.

It does do something... return an arithmetic mean.

return float(sum(numbers)) / len(numbers)

Yes it is. The function takes an object (a dictionary), and enumerates the three properties that have data structures (lists). Each category is given a weighting factor (weighting factors add to 1), and the sum of the three outcomes is returned.

Yes, if we call the function on students. The list is then iterated and each student object passed to the get_average() function. These results are tabulated and an arithmetic average computed on the return value.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.