Student Becomes the Teacher Finale


#1

https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/5?curriculum_id=4f89dab3d788890003000096

The result I get is as follows:

Lloyd:
80.55
B

Alice:
91.15
A

Tyler:
79.9
C

Class average:
None
None
None
83.8666666667
None
None
None
C
None

This is a "correct" result... but I must ask: Why do I get a total of seven lines of "none"?
And why do my code print a class average of "C" when it should be "B"?

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

students = [lloyd, alice, tyler]

def average(numbers):
    total= float(sum(numbers))
    result = total / len(numbers)
    return result

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):
    score = get_average(student)
    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:
        print results.append(get_average(student))
    return average(results)

for student in students:
    print student["name"]+":"
    print get_average(student)
    print get_letter_grade(student)
    print " "

print "Class average:"
print get_class_average(students)
print get_letter_grade(get_class_average(students))

#2

Not I follow your reasoning, in the above. Why redefine the score parameter?


#3

I do redefine the score parameter because if I dont, all of the students grades would be "A". And if i dont redefine the score parameter, the class average grade becomes correct (which is "B")


#4

What do you call this?

def get_letter_grade(score):
    score = get_average(student)

I don't follow your reasoning.


#5

I edited my reply, my english is not that good.


#6

What if you call get_letter_grade with,

get_letter_grade(get_average(student))

That way you are passing a value, and not ignoring or redefining the parameter.


#7

THANK YOU! I am so tired... =D How could I not think of this! =D


#8

When I find myself getting confused I change my activity for a little while. Get up, go for a walk, make a snack, or even just take a nap. This stuff isn't going to run away and nobody is timing us. Go easy on yourself and expect to make mistakes. It's all part of learning.


#9

That is a good advice! My first question remains unanswered tho, why do I get seven lines of "none" in my result?


#10

Hi,
You get seven lines of "none" is because of this line of code in your get_class_average(students) function in your for loop
print results.append(get_average(student))

The first three none's are there when you call the function, inside the function, there is a for loop which loop 3 times. In this for loop, you have the print function.

The next three none's are there is because you print get_class_average(students). So basically you print it twice. To get rid of it, you can just remove the word 'print' in your get_class_average (students) function.

The last None is always there whenever you're done with the exercise.

You got C instead of 'B' is because of this line of code in your get_letter_grade(score) function:
score = get_average(student)
It means that you already pass some value to 'score'
Then, you call the function at the end so it takes the old value plus the new value and gives you the wrong result.
You just need to remove score = get_average(student) in get_letter_grade(score) function.

Good luck :slight_smile: