Alice Error, Part of the Whole 8/9


#1

I've been struggling with this one. It says: get_class_average([alice]) returned 9.75 instead of 91.15 as expected.

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

#2

Hi,

I can't find your get_class_average() function, and get_average() is defined twice for some reason.

This doesn't make sense to me:

because you compute e.g. homework* 0.1 but don't do anything with it.

Also:

A function exits after a return statement is executed, so the second and third return statement here are never reached.

Try to use the "Preformated text" button to make your code more readable.


#3

Hi eveat,
That makes sense. I copied it from someone else on the forum and I thought this was right. I tried a bunch of different things and came up with the following but I'm getting a 'return' outside function error on the last line: return average(results).


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

#4

Please format your code (select it; press Ctrl+Shift+C) from next time on wards. Thanks! :slightly_smiling:

As to your new code sample:

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

the indentation is way off. Both the for loop body and return statement need to be inside the function - 4 spaces to the right. Currently, the return statement is outside the function, and hence you are getting the error - 'return' outside function


#5

I'm still not sure how to indent


#6

A return statement only occupies its place inside of a method. For example, the following:

return 5

makes no sense unless you put it in a method:

def get_number(flag):       // zero indent
    print "Starting process"// four space indent
    return 5                // four space indent

However, Python is really strict about indentation. If you don't indent any statement, that's supposed to be inside the method, four spaces to the right of the indent of the method definition, it wouldn't be treated as a part of the method.

So, in the example above, the method definition for get_number has initial indent of zero spaces. Add four to it to get four spaces. Thus, your return statement (and any other statement that should be a part of that method) should be at four spaces indent.

Hope it helps!


#7

Hi gaurangtandon,
sorry for responding late, just figured out what I needed to do. Thanks for the help!