Stuck with the error :( with lesson 9


#1

Hello,

For the fist time, I'm blocked facing the 'Oops, try again ....'. I tried and tried. Adding some 'debug print', here are my results:
**get_average: Lloyd 80.55
1 {'quizzes': [88.0, 40.0, 94.0], 'tests': [75.0, 90.0], 'name': 'Lloyd', 'homework': [90.0, 97.0, 75.0, 92.0]} [80.55]

get_average: Alice 91.15
2 {'quizzes': [82.0, 83.0, 91.0], 'tests': [89.0, 97.0], 'name': 'Alice', 'homework': [100.0, 92.0, 98.0, 100.0]} [80.55, 91.14999999999999]

get_average: Tyler 79.9
3 {'quizzes': [0.0, 75.0, 78.0], 'tests': [100.0, 100.0], 'name': 'Tyler', 'homework': [0.0, 87.0, 75.0, 22.0]} [80.55, 91.14999999999999, 79.9]

score of the class: 83.8666666667
grade of the class: B**

And here is my code:
indent preformatted text by 4 spaces

(...)
# Average computation
def average(numbers):
    total = float(sum(numbers))
    total = total / (len(numbers))
    return total

#Weighted average
def get_average(student):
    homework = average(student["homework"])*0.1
    quizzes = average(student["quizzes"])*0.3
    tests = average(student["tests"])*0.6
    
    #waverage = sum([homework, quizzes, tests])
    #waverage = homework + quizzes + tests
    print "get_average: " + student["name"], sum([homework, quizzes, tests])
    return sum([homework, quizzes, tests])

#assign grade based on the score
def get_letter_grade(score):
    if type(score) == int or type(score) == float:
        if score >= 90:
             return "A"
        elif score >= 80:
            return "B"
        elif score >= 70:
            return "C"
        elif score >= 60:
            return "D"
        else:
            return "F"
    
    else:
        return "Error"

#print get_letter_grade(get_average(lloyd))

#avergae of the class
def get_class_average(students):
    results = []
    ind = 0
    for student in students:
        ind = ind + 1
        results.append(get_average(student))
        print ind, student, results, "\n"
        
    return average(results)

classscore = get_class_average([lloyd, alice, tyler])
classgrade = get_letter_grade(classscore)

print "score of the class: ", classscore
print "grade of the class: ", classgrade


#2

Hi this part bewtween

......
#print get_letter_grade(get_average(lloyd))

#here

#avergae of the class
def get_class_average(students):
........

just add

students = [tyler, alice, tyler]

#3

why ind = 0, and increment by 1?


#4

Thanks, 'ind' is just used to make the view of the print more readable ... For my comfort only :yum:


#5

Sorry, I don't understand why I should redefine the list with tyler x 2!
The detailed results I've got are:
`

get_average: Lloyd 80.55
1 Lloyd [80.55]

get_average: Alice 91.15
2 Alice [80.55, 91.14999999999999]

get_average: Tyler 79.9
3 Tyler [80.55, 91.14999999999999, 79.9] 

score of the class:  83.8666666667
grade of the class:  B`

#6

Sorry one should be lloyd


#7

Hi Folks,

Here is a copy of my python log window and adding the following line
def get_class_average(students):
students = [lloyd, alice, tyler]

didn't change anything, as my call (classscore = get_class_average([lloyd, alice, tyler])) does the same. Anyway, thanks

1 Lloyd get_average:  80.55
2 Alice get_average:  91.15
3 Tyler get_average:  79.9
get_class_average results: [80.55, 91.14999999999999, 79.9]  =  83.8666666667

=> Score of the class:  83.8666666667
=> Grade of the class:  B

#8

It shouldn't be inside the function ...it should be

students = [lloyd, alice, tyler]

def get_class_average(students):

#9

Thanks for your inputs and advises. Yes, it wasnt inside the function but I tried in case of to redefine it ... Both versions are giving the same result.
Have a great day anyway. I'll look more into details for other's concerns and troubles. Maybe I'll find the solution :slight_smile:
#avergae of the class
def get_class_average(students):
results = []
ind = 0
for student in students:


#10

Okayyy ..... I used the code form someone else in this forum, and it works ...... but with the same result. Frustrating because not understood, but at least I'm able to move on ....

Source: https://discuss.codecademy.com/t/student-becomes-the-teacher-how-is-everybody-doing/37666/4