The Letter Grade is always printed as an "A"


Student Becomes the Teacher: How is Everybody doing?

Oops, try again. Did you remember to print the letter grade for the class's average?

I am getting the class average to be 83.86, however the class average letter grade is printing to be an A, even though it should be a B. Also, all individual student grades are printing as an A.

I expect the correct letter grade to print.

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 = sum(numbers)
    total = float(total) / len(numbers)
    return total
def get_average(student):
    homework = average(student['homework'])
    quizzes = average(student['quizzes'])
    tests = average(student['tests'])
    tot = 0.1 * homework + 0.3 * quizzes + 0.6 * tests
    return tot
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80 and score < 90:
        return "B"
    elif score >= 70 and score < 80:
        return "C"
    elif score >= 60 and score < 70:
        return "D"
        return "F"
students = [lloyd, alice, tyler]  
def get_class_average(students):
    results = []
    for student in students:
        avg = get_average(student)
    return average(results)
print get_class_average(students)
print get_letter_grade(students)


get_letter_grade() function does not take list as argument but takes score,a number.
How you get score ?
by using get_class_average() function and passing it argument and pass this value to get_letter_grade().


I'm sorry, but I don't completely understand your comment. How do I get the get_letter_grade() function to take a number as its argument? I tried print get_letter_grade(score), and it printed

Traceback (most recent call last):
File "python", line 54, in
NameError: name 'score' is not defined


It's okay.

There are four functions in this program.

average(numbers) - Takes list of numbers and gives us average.

get_average(student) - takes any student (dictionary) and it uses average() function to calculate average of student's homework,quizzes , tests and gives you weighted average.

get_letter_grade(score) - takes a number and gives grade on the basis of score.

get_class_average(students) - takes a list containing dictionaries (tyler,alice and lloyd) and gives class average,It uses get_average() function.

Now back to your error:

score is not defined yet.
You can find score using your get_class_average(students) function.
It will give class average ,then you can pass this value to your get_letter_grade() function and It will give you grade of the class.


That worked! Thank you so much!


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