Seemingly incomplete code still works


#1



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

and

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


No error, wondering why. I’ve been messing around with the code since I got confused by step 3 in the first link:

"For each student item in the class list, calculate get_average(student) and then call results.append() with that result."

I still have no idea what "class" "list" that is refering to (finished the chapter, incidentally). I’m assuming in this case it means the concept, rather than Python class? As in, the "students" list of dictionaries I made earlier, containing the three in the example (lloyd, alice, tyler).

Anyway, the problem is, I deleted that list while trying to figure out the class thing yet the code still works and lets me pass. "get_class_average" takes it as an argument, both print(type(students)) and print(students) at the end return the respective values of the locally non-existent list (copy-pasted it into PyCharm, students is an unresolved reference there). Is it because the list is globally available from somewhere? My thanks to anybody patient enough to clear up where I’ve gone wrong here.


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]
    }

def average(numbers):
    total = float(sum(numbers))
    result = total / len(numbers)
    return result

def get_average(student):
    homework_average = average(student["homework"])
    quizzes_average = average(student["quizzes"])
    tests_average = average(student["tests"])
    overall_average = homework_average*0.1 + quizzes_average*0.3 + tests_average*0.6
    return overall_average

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)))
print(type(students))
print(students)


#2

Even though the students list (this is the "class list" being referred to) was deleted from your code, it is possibly still present in memory from an earlier run of the program. Save your code and refresh the page. Now students will no longer be in memory so should raise an exception when you run this code.

Add the students list again, and all will be well.


#3

I thought it might be something like that, and you are indeed correct. Thank you for answering.


#4

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