8/9 Part of the Whole


#1

I have no idea what I've done wrong

This is my code

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

This is my error code

 `Traceback` (most recent call last):
      File "python", line 64, in <module>
  File "python", line 60, in get_class_average
  File "python", line 26, in get_average
TypeError: string indices must be integers, not str


I've looked over the code multiple times and I don't know what I have typed wrong. Could I have some help, please?


#2

this line:

print get_class_average(Llyod)

when calling the function, the argument provided, should contain a list, since you are going to loop over it. so then it should be:

print get_class_average([llyod])

the name of the dictionary is lloyd, with all lowercase letters, which is what you have to use here. then there is a problem in this line:

return average(results)

the return is currently indented in such a way, that it is reached in the first run of the loop, the moment a return keyword is reached, a function ends, maybe you should change the indent, so the for loop can append all student averages to the list?


#3

I've made some improvements, this is my code now:

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

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

#print get_average([lloyd])
print get_class_average(get_average([lloyd]))

Now, the error message I get is:

Traceback (most recent call last):
  File "python", line 50, in <module>
NameError: name 'results' is not defined

This is line 50:

print average(results)

#4

please post your whole code, with just this i can't debug the issue


#5

Here it is

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

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

print get_class_average(get_average([lloyd]))

#6

your function call at the last line:

print get_class_average(get_average([lloyd]))

get_average() is not designed to handle a list, it expects a dictionary.

but then get_class_average is not designed to handle what you just did, i suggest to remove this line for now, until you get to the next exercise where this is explained, so you can make correct function calls


#7

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