Part of the Whole


#1

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

On line 46, the console returns a syntax error at the end of the “class”. The class list also appears in purple for some reason?



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 = 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"
print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    results = []
    for student in class:
        results.append(get_average(student))
        return results



#2

class is a reserved keyword, as you can see by the purple color in the editor

the exercise on purpose didn’t tell you what exactly to loop over, we have progressed so far in the course, that you shouldn’t just copy paste from the instructions, but should think about the design of your program, so what should we loop over?


#3

The exercise specifically states that the list should be called “class”. I have no idea what the list “should” be called at this point, and I find myself more than a little confused.


#4

no, like i said, as we can further into the course we go from spoonfeeding/coding to programming. So yes, the exercise is on purpose putting in a thinking step for you, to make you aware of what you are actually doing. After reading step 2, read step 1 of the instructions again:

You can expect students to be a list containing your three students.


#5

I changed the class list to one called students. I now get the message that 91.15 was returned instead of 85.85 as expected. Any suggestions?


#6

I also changed return results to return average(results)


#7

good :slight_smile: This is why reading, pausing and thinking about the instructions is very important. Programming is mostly thinking and designing, coding is just a small part

also good

Now, a function ends the moment a return keyword is reached (even when return keyword is reached in the loop), so your function ends in the first iteration of the loop. So it can never calculate the correct class average for a class of 2 or more students, given the whole loop needs to run (make all its iteration) to get to the correct class average


#8

Ah I see, the return was part of the for loop. Removed the indentation and it works now. Thanks for the help!


#9