8/9 Part of the Whole


#1

I've been having trouble with my code for a while, but I'm getting this error message:
"get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected"
Please help! Everything seems right to me. Am I missing any details?
Here's my code:

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']) * 0.1
quizzes = average(student['quizzes']) * 0.3
tests = average(student['tests']) * 0.6
return homework + quizzes + 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 average(results)


#2

Maybe something is improperly indented. That same code (indented properly) works perfectly for me.


#4

I had the same code as you do as well. I fixed the error you were receiving by making sure that the final line (the return statement) is out side of your "for" loop. Backspace so that the return is lined up with "results" and "for". Hope this helps you!


#5

you want to put an extra line saying
results = average(results)
return results


#6

Hi
I got the error like this:
Oops, try again. get_class_average([alice]) resulted in an error: string indices must be integers, not str

My code is below:
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)
tbc=total/len(numbers)
return tbc
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=[]
xxx = ["lloyd ","alice","tyler"]
for students in xxx:
results.append(get_average(students))
results= average(results)
return results

Does anyone help me fix it?
Many thanks in advance.


#7

@linhnguyen2412gmail_

First off, in your get_class_average method there's no need to have xxx = ["lloyd ","alice","tyler"]. The list that you need for this function is passed to it as an argument, students.

Also, make sure you use a different name for the 2 different results. You shouldn't make a list called results and then make a float also called results in the get_class_average function. Rename the variable defined as results = average(results). Also remember to change the return statement to return that variable.

Hope this helps!


#8

Indentation of "return" was the problem! Everything works now!


#9

Thank you very much for this message. It really helped me out too. I just don't understand why we don't have to define a list with the students in it. How does Python make the link between the argument of our function (students) and the three students? Any further insight on this would be really appreciated. Many thanks!


#10

Thanks @javamaster39858, I'm glad I can help. :grin:
You don't need to define a list with the students in it because when you call any of the functions, you're giving it the student it needs to complete its task.
For example, if you call get_average(lloyd) you're giving the function get_average all of the information it needs as its argument, which is the lloyd dictionary.


#11

Why does results.append(get_average(student)) work but results.append(student) does not work? in the question it states to calculate get_average(student) and then call results.append() with that result. How does it work in a two step process? I tried

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

and it did not work but the code below did.

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

#12

Inside the while-loop in the first example you are not storing or doing anything with the return value from the get_average(student) function. You then append student (a dictionary) at the end of the result list.

Inside the while-loop in the second example you also call the get_average(student) function without using the return value for anything. After that you append the return value of the function get_average(student), the students average, to the result list.

You should have written the first example more in the style of

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

Or in the second example it should have been

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

I'd say both of the alternatives look pretty good. The first one is a bit better when it comes to readability and the second one is a bit more compact.


#13

@uruichi You have to call call results.append(get_average(student)) because your finding the average of all of the students. First the average of the student is found using get_average(student) and then, after the for loop, you find the average of all of the averages with average(results). If you just call results.append(student) then you're just making a list of all of the students and then returning the average of the students instead of the average of the averages.


#14

@codeninja84962, I see so creating the average variable then working off of that would have worked. And @doublediamond I missed the average of averages THANK YOU BOTH!!


#15

This worked for me, thanks ruby.


#16

this is my code and i have similar problem
im getting this message: Oops, try again. get_class_average([alice]) resulted in an error: global name 'student' is not defined

   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)
        total = total /len(numbers)
        return total
        print total
    def get_average(student):
        homework = average(student["homework"])
        quizzes = average(student["quizzes"])
        tests = average(student["tests"])
        results = (homework * 0.1) + (quizzes * 0.3) + (tests * 0.6)
        return results
    
    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 s in students:
            results.append(get_average(student))
            
        return average(results)

#17

nevermind i found the mistake

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

it should be

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

#18

This worked for me as well.. Exact same situation


#19

works for me on the second code..thanks code ninja..


#20

hello iamrmp, I had the same code as you, but I noticed your indentation just after your for loop in get_class_average is wrong. the results.append should be indented a little bit with the for loop.


#21

hi,

"students" is not defined as a variable in this lesson, how does python know to pull info from lloyd, alice, and tyler?

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