8. Part of the Whole Average error


#1

I'm not sure what is wrong with the code, but I keep receiving the following error:

get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected

Please can someone help?

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 = float(sum(numbers))
    result = total / len(numbers)
    return result
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    result = 0.1 * homework + 0.3 * quizzes + 0.6 * tests
    return result

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)

#2

It is hard to troubleshoot code that is not properly formatted especially in a language like python where the language works based on indentation and white space. It would be a lot easier to help you if you posted your code with proper formatting or even a screen shot. Otherwise we have to guess what your problem is.


#3

the problem to the code is we define results=[] before we define the function... id we difine t inside it works like a baby,...

wrong code:
results=[]

Heading

def get_class_average(students):

for student in students:
    results.append(get_average(student))
    #print student
    #print results
return average(results)

right code:
def get_class_average(students):
results=[]
for student in students:
results.append(get_average(student))
#print student
#print results
return average(results)


#5

how could i display the code as it appears on the code console?


#6

paste the code in, then highlight it and click on the icon that looks like this: </>

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 = float(sum(numbers))
    total = total / len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    
    return 0.1 * average(student["homework"]) + \
    0.3 * average(student["quizzes"]) + \
    0.6 * average(student["tests"])
    
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 results
        
get_letter_grade(get_average(lloyd))

#7

Please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:

and it is clear from your now formatted code that return average(results) statement in the get_class_average method is inside of the loop, and so breaks on the very first iteration. So, make it inside of the method by changing the indentation.


#8

Yeah i was stuck on that one for a while but i got there in the end on my own, which is far more satisfying! I just used the code that i was on at the time to explain how to format it properly in this forum.

Thanks anyway though :wink:


#9

I started with the error at the top: "get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected" when I ended the get_class_average function with "return average(results)"

I've used this exact code, the only mod being to end with "return results" and the error is now:
Oops, try again. get_class_average([alice]) should return a number