8. part of whole error


#1

I'm executing my code and get the error: > Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected

I don't know what is wrong with it. The code is:

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

classlist = [lloyd,alice,tyler]

def average(person):
total = sum(person)
avarage = float(total) / len(person)
return avarage

def get_average(student):
homework = average(student["homework"])
quiz = average(student["quizzes"])
tests = average(student["tests"])
return 0.1 * homework + 0.3*quiz + 0.6*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"
print

get_letter_grade(get_average(lloyd))

def get_class_average(student):
result = []
for student in classlist:
avg = get_average(student)
result.append(avg)
return average(result)

Can anyone tell me what is the problem with my code?


#2

The parameter should be 'students`. and,

for student in students:

#3

Unfortunately that is not it. I named my studentlist classlist (students is not defined in my code) so that part of the code is correct I believe. When I run your code I get the same error.


#4

Had the same problem and i just removed the "classlist" or "students" list.
I guess the test will use "students" and you do not have to define it in your 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"])
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"

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


#5

I've changed my code to be almost exactly like that and it still isn't working.. my error message is :

File "python", line 54
SyntaxError: 'return' outside function


#6

This error message indicates that a return statement has been found that is not in a function block.

    def func():
        # code
        return something

#7

thank you!! after fumbling with the code more I realized it was a spacing issue!


#8

what does it look like?


#9

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 / float(len(numbers))
    return result
    
    def average(numbers):
        total = float(sum(numbers)) / len(numbers)
    return total

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"
    elif score < 60:
        return "F"
print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

print get_average(lloyd)
print get_average(alice)
print get_average(tyler)

students = [lloyd, alice, tyler]

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

#10

This is how i completed the task, there may be a different or even a better way of completing this exercise.

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 / float(len(numbers))
    return result
    
    def average(numbers):
        total = float(sum(numbers)) / len(numbers)
    return total

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"
    elif score < 60:
        return "F"
print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

print get_average(lloyd)
print get_average(alice)
print get_average(tyler)

students = [lloyd, alice, tyler]

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

#11