8. Part of Whole


#1


Oops, try again. get_class_average([alice]) resulted in an error: can't multiply sequence by non-int of type 'float'

Hi, i have a bug as above, and I cant find any solution. I was even trying to copy codes from another people and nothing works. Do I have a mistake in previous task or what? Thanks for 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 = 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'])
    weighted = ("homework" * .1) + ('quizzes' * .3) + ( 'tests' * .6)
    return weighted
    
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
    else:
        return "F"
    print get_letter_grade(get_average(lloyd))
    students = [lloyd, alice, tyler]
      
    def get_class_average(students):
        results = []
        for student in students:
            results.append(get_average(student))
    return average(results)


#2

do you ever call the function?


#3

Double check your for loop and what it does. your code for it looks good but it is missing something.

Your Code:

results.append(get_average(students))

also make sure you spell the code just as it says in the instructions and your code properly matches the arguments for your functions.


#4

This jumped out so quickly I chose not to mention it, yet. But it is still not coming up in replies.


#5

Huh? It appends each return value to that list. Perfectly normal.

When results contains all the weighted averages, it is ready to average and return. The code itself looks fine but for the return line.


#6

@mtf
you are correct I was looking at the wrong reference code.

@mrokuproinformatyk
check the instructionsfrom the lesson again pay close attention to spelling


#7

D'oh! students. No, that is not normal operation and what will throw an error every time. How the heck do we miss this stuff?

results.append(get_average(student))

The most common error. Must be getting desensitized to it. Sheesh! My apologies.

Wait. That was you who threw that in. I was duped! LOL.


#8

Thank you for replies. I've edited this line:
results.append(get_average(students))
my bug from the topic disappeared but now I have new one.
"Oops, try again. get_class_average([alice]) resulted in an error: list indices must be integers, not str"

I have seen this bug report in other topics but I still can't solve this one.


#9

That line should be,

results.append(get_average(student))

Singular student, the key variable in your loop.


#10

so I still have this bug :Oops, try again. get_class_average([alice]) resulted in an error: can't multiply sequence by non-int of type 'float'


#11

Please repost your get_class_average code. Thanks.


#12

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

#13

There is the issue. student is defined in your function as the key variable. students is the object being iterated, which should be the parameter.

def get_class_average(students):

This way the function is getting passed a list, not a dictionary.


#14

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

return average(results)

changed it as you say, but I have still this bug:
Oops, try again. get_class_average([alice]) resulted in an error: can't multiply sequence by non-int of type 'float'


#15

I have found on forum code for this task which worked for someone else and its the same as mine, but its not working for me. Is it possible that I have passed previous task with mistake?

students = [lloyd, alice, tyler]
def get_class_average(students):
results=[]
for student in students:
results.append(get_average(student))

return average(results)

#16

I have no doubt that indentation is an issue, but we cannot tell since your post is not formatted.


#17

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

#19

The problem goes all the way back to this line. The variables should not be in quotes.


#20

Hey, i'm pretty sure that the whole function should not be inside another function.
def get_class_average(students):
is inside of
def get_letter_grade(score):


#21

I think that issue has been addressed. Everybody has been trying very hard to not give away the solution, and after 20 posts, we can hope this topic is resolved.