8. Part of the whole


#1

I have something wrong with my code. Please anyone can help me to find where is the mistake?

Here is the link to my exercise:

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

The error messages were:

Traceback (most recent call last):
File "python", line 47, in
File "python", line 36, in get_letter_grade
NameError: global name 'student' is not defined

The activity requires the following:

Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.
First, make an empty list called results.
For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
Finally, return the result of calling average() with results.

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)
    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) #retirar parenteses
    return (0.3*quizzes)
    return (0.6*tests)
    
def get_letter_grade(score):
    score = get_average(student)
    if score >= 90.0:
        return "A"
    elif score >= 80.0:
        return "B"
    elif score >= 70.0:
        return "C"
    elif score >= 60.0:
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

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

#2

I am pretty bad at this so my explanation may not be 100% correct. But you have to make a variable = get_average(students) and then use that variable to append results. Also you need to return after the for loop is completed

so we will use a variable called "ff"

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

#3

Thank you for the help.

I' ve made the changes you described but it shows the same error message:

Traceback (most recent call last):
File "python", line 47, in
File "python", line 36, in get_letter_grade
NameError: global name 'student' is not defined

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)
    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 #retirar parenteses
    return 0.3*quizzes
    return 0.6*tests
    
def get_letter_grade(score):
    score = get_average(student)
    if score >= 90.0:
        return "A"
    elif score >= 80.0:
        return "B"
    elif score >= 70.0:
        return "C"
    elif score >= 60.0:
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

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

#4

here:

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

score is already the average score, why overwrite the function parameter

here:

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return 0.1*homework #retirar parenteses
    return 0.3*quizzes
    return 0.6*tests

a function ends the moment a return keyword is reached, so you never return the correct weighted average


#5

Thank you so much guys, now it works!

Here's the 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]
}

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    result = total / len(numbers)
    return result
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    homework = 0.1*homework #retirar parenteses
    quizzes = 0.3*quizzes
    tests = 0.6*tests
    media = sum([homework, quizzes, tests])
    return media
    
def get_letter_grade(score):
    #score = get_average(student)
    if score >= 90.0:
        return "A"
    elif score >= 80.0:
        return "B"
    elif score >= 70.0:
        return "C"
    elif score >= 60.0:
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

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

#6

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