8/9 Part of the Whole - my code is not wrong


#1

I'm the lucky millionth person stuck on "Part of the Whole":
https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

I didn't have trouble with this lesson. It was clear what I had to do and I didn't mess up any tabs. My code is exactly, verbatim, what answerers have given as the correct answer, in dozens of posts over the years - the same correct code that askers have exultantly proclaimed to finally work and get them past this lesson. Mine isn't working. It's not wrong either - I can make this work with a text editor and the real Python program.

The code below returns the following error message:

"Oops, try again. get_class_average([alice]) returned 85.6875 instead of 91.15 as expected"

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)
    avg = total / len(numbers)
    return avg
    
def get_average(student):
     homework = average(student["homework"])
     quizzes = average(student["quizzes"])
     tests = average(student["tests"])
     return homework*.1 + quizzes*.3 + tests*.6
    
students = [lloyd, alice, tyler]

results=[]

def get_class_average(students):
    for student in students:
        results.append(get_average(student))
    return average(results)

get_class_average(students)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You'll note that my tab for return average(results) is not incorrect.

Again, it returns the message:
"Oops, try again. get_class_average([alice]) returned 85.6875 instead of 91.15 as expected"

Interestingly, if I print the students list, something weird happens:

[{'quizzes': [88.0, 40.0, 94.0], 'tests': [75.0, 90.0], 'name': 'Lloyd', 'homework': [90.0, 97.0, 75.0, 92.0]}, {'quizzes': [82.0, 83.0, 91.0], 'tests': [89.0, 97.0], 'name': 'Alice', 'homework': [100.0, 92.0, 98.0, 100.0]}, {'quizzes': [0.0, 75.0, 78.0], 'tests': [100.0, 100.0], 'name': 'Tyler', 'homework': [0.0, 87.0, 75.0, 22.0]}]

You'll see that Tyler has been cut off after homework, and inserted near the beginning. I've done the entire lesson perfectly word-for-word from the beginning, twice, and it's nothing I've done causing this. Not sure if this is affecting my results, but I haven't seen anyone else have this issue or notice this issue in the YEARS that people have been stuck on this terrible lesson.

This has wasted far too much of my time.

Thanks in advance, for anyone kind enough to answer yet another question on this lesson.

***If I skip ahead, MY CODE WORKS on the next lesson. No problem. Vigorously shaking my head at how poorly done 8.9 is. Never before have skipped a lesson. Luckily I'm not a religious completionist. I'll have to file this away with the Koroks.

`


#2

look at this code:

results=[]

def get_class_average(students):
    for student in students:
        results.append(get_average(student))
    return average(results)

get_class_average(students)
get_class_average([lloyd])
get_class_average([lloyd, alice])

calling the function multiply times causes problems because results is not defined inside the function, so the list will accumulate the numbers in list from all the function calls, causing an incorrect class average


#3

Unfortunately, your code is incorrect. Here:

results=[]

def get_class_average(students):
    for student in students:
        results.append(get_average(student))
    return average(results)

The definition for results should go inside the function definition for get_class_average().

In other words:

results=[] #This should be moved from here

def get_class_average(students):
    #To here
    for student in students:
        results.append(get_average(student))
    return average(results)

#4

Much appreciated - I had it within the function in the beginning, but in the scramble of trying to make things work I moved it and never put it back, without realizing it made a difference. I can see why it does.

I must have had something else wrong in the very first place, though I'm not sure what.

Thanks again!


#5

Yep, I moved it early on when I was trying to make things work - I must have had something wrong very early on in my attempts and not realized it, then come across the otherwise correct function code without re-placing the results function.

Thanks!


#6

2 posts were split to a new topic: 8/9 part of a whole


#7

Thanks for that. I tried it .It works just fine.


#8

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