Part of the Whole


#1



Okay, so I'm stuck. When I run my code, I get an error message: "Oops, try again. get_class_average([alice]) resulted in an error: unsupported operand type(s) for +: 'int' and 'str' "


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 get_average(numbers):
    total = float(sum(numbers))
    average = total / len(numbers)
    return average
    
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 score
    
def get_class_average(students):
    results = []
    for each in students:
        get_average(each)
        results.append(average(each))
    return get_average(results)


#2

i think you misunderstand instruction
For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
so you have to append get_average(each) to result list not average(each)


#3

also you code need function get_average() and you name average() by get_average() !!!
so this my code for 8.Part of whole

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)
    #print total
    averag=float(total)/len(numbers)
    #print averag
    return averag
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    somm = homework*.1 + quizzes *.3 + tests *.6
    return somm
def get_letter_grade(score):
    if score>=90.0 :
        return "A"
    elif score>=80.0 :
        return "B"
    elif score>=70.0 :
        return "C"
    elif score>=60.0 :
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))
def get_class_average(students):
    results=[]
    for x in students:
        y=get_average(x)
        results.append(y)
    return average(results)

#4

I've adjusted my code slightly...

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

That's the portion I've changed. I'm still getting the same
" Oops, try again. get_class_average([alice]) resulted in an error: unsupported operand type(s) for +: 'int' and 'str' " error that I was getting earlier. I just copy/pasted the piece of code I changed. The rest is the same as it was before. So what am I screwing up?


#5

This is how I did it and it worked.

students = [lloyd, alice, tyler]

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


#6

I'm getting this error " Oops, try again. get_class_average([lloyd]) returned 85.85 instead of 80.55 as expected "

even though i got 80.55 as expected

here is 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]
}

students = [lloyd, alice, tyler]
results = []

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    out = (0.1*homework) + (0.3*quizzes) + (0.6*tests)
    #out = ((homework) + (quizzes) + (tests)) / 3
    return (out)
        
def average(numberl):
    totally = sum(numberl)
    totally = float(totally)
    length = len(numberl)
    return totally / length
    
def get_letter_grade(score):
    if score > 89:
        return "A"
    elif score > 79:
        return "B"
    elif score > 69:
        return "C"
    elif score > 59:
        return "D"
    else:
        return "F"
        
def get_class_average(students):
    for stu in students:
        output = get_average(stu)
        print output
        results.append(output)
    result =  average(results)        
    return result

Thanks in advance. help me out of this!!



#7

Hi its missing that on you code

2 First, make an empty list called results.


#8

Just curious, in the line:

in your get_average function, where do 0.1, 0.3, and 0.6 come from?


#9

I think they are the percentages


#10

it was given in earlier steps


#11

It was there in the previous steps :slight_smile:


#12

Hi,
I have added
results = []
is it wrong?


#13

Hi no it's not wrong .


#14

I did tried doing the same thing but it didn't work.
It states:

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

#15

i've got the same issue, some one kindly help us out


#16

I get "Oops, try again. get_class_average([alice]) resulted in an error: global name 'average' is not defined" ?


#17

Hi
Thanks @wizmarco
but same error message :frowning:


#18

Hi! I modified my code to match your pattern and it actually worked! One thing I don't understand is why it accepted students as a list even though it wasn't defined beforehand... I kept getting an error because I made a list (class_list) and used it in the for loop instead of students, but when I changed it to students, it accepted my code

here's the modified code:

 class_list = [lloyd, alice, tyler]

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

#19

Hi can you post your code?


#20
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]
}

students = [lloyd, alice, tyler]
results = []
# Add your function below!
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    out = (0.1*homework) + (0.3*quizzes) + (0.6*tests)
    #out = ((homework) + (quizzes) + (tests)) / 3
    return (out)
        
def average(numberl):
    totally = sum(numberl)
    totally = float(totally)
    length = len(numberl)
    return totally / length
    
def get_letter_grade(score):
    if score > 89:
        return "A"
    elif score > 79:
        return "B"
    elif score > 69:
        return "C"
    elif score > 59:
        return "D"
    else:
        return "F"
        
def get_class_average(students):
    for stu in students:
        output = get_average(stu)
        print output
        results.append(output)
    result =  average(results)        
    return result