Student Becomes the Teacher - Part of a whole


#1

any help is much appreciated, the code executes but the result is incorrect

Traceback (most recent call last):
File "python", line 57, in
File "python", line 53, in get_class_average
File "python", line 29, in get_average
TypeError: string indices must be integers, not 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]
}

total = 0 
def average(numbers):
    total = sum(numbers)
    total = float(total)
    x = total / len(numbers) 
    print x
    return x
    
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_average(lloyd)

#print get_letter_grade(get_average(lloyd))

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

#2

here:

print get_class_average(lloyd)

when you call the function, the argument should be a list. In your case, its a dictionary. Which creates problem with loop and calling get_average inside get_class_average


#3

ok well after being away from my computer for a while I'm still not getting the correct value please be more descriptive. I only put in the print statement to help me identify the errors

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

total = 0 
def average(numbers):
    total = sum(numbers)
    total = float(total)
    x = total / len(numbers) 
    print x
    return x
    
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_average(lloyd)

#print get_letter_grade(get_average(lloyd))

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

#4

a function ends the moment return keyword is reached

if a return keyword is reached in the loop, the loop will break

so your loop breaks in its first iteration


#5

ok but I already knew that, what I'm struggling with the following error message

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


#6

if you know that, you should be able to solve the problem

get_class_average([alice, lloyd])

this, currently calculates class average for alice, given the loop breaks in first iteration


#7

sorry I didn't mean to come across as snappy, I used to have another block of code that they removed, and I'm just frusterated

students = [lloyd, alice tyler]

why did they get rid of this code?


#8

yea, students doesn't get properlied transfered to the next exercise at some point


#9

ya but i tried adding that back in and i get the same error


#10

but that is not the problem, this is:

in the first iteration of your loop, the loop breaks

thus, your function is unable to calculate a correct class average for a class of 2 or 3 students


#11

crap I was beating myself up on this and turns out that the return was not indented to allow the loop to cycle through the students

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

does this look ok? it worked


#12

yes, that is good :slight_smile:


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.