11.9 "How is everybody doing?" -- Where is 'students' defined?


#1

https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/5?curriculum_id=4f89dab3d788890003000096

I'm trying to follow along with the logic of this operation. Someone straighten me out, please.

Successful code is:

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}

alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}

tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total/len(numbers)
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return homework * 0.1 + quizzes * 0.3 + tests * 0.6
    
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
        
def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)
    
print get_class_average(students)

print get_letter_grade(get_class_average(students))

I just don't understand how that works. Let me describe what I see is happening, and someone please jump in to tell me where I'm getting it wrong.

0 we define our dictionaries, and our functions. (lines 0-51)
1 call to print the result of the function get_class_average of argument 'students'. (line 52)
2 in that function, we have an empty list called 'results' (line 47)
3 for every item in a list called "students" (line 48), add the student's average to 'results'. (line 49) is this the key? if so, we're kind of implicitly defining it.
4 once you've averaged each student, then average all of them together. (line 50)
5 now that you have the answer, print it out (line 52)
6 now do it all again, but express it as a letter grade (line 54)

Except that's not true.
Paste in my code.
Change every instance of "student" to "cat" and "students" to "cats".
Run the code.
The program will fail and say that "cats" is not defined.
Change it back to student/students and it runs fine.

So where exactly in this code are we defining students/cats/whatevers?


#2

actually, this program doesn't run. I think your browser might be sending cached data after so many submits.

students on line 52 isn't defined anywhere. You did make a students list in 3. put it together, this list should also be present in this exercise


#3

Thank you so much!
I could NOT figure out where it knew what 'students' was.
Feel free to delete the post if you don't think it adds anything.


#4

#5

i think it might be useful for other people, we normally don't delete topics unless we have good reasons (spam, foul language)