How is everybody doing


#1



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


One of the following is missing or broken when we tried to use it: alice, lloyd, tyler, students, get_class_average, get_letter_grade


I expect for the code to work perfectly fine.


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]
}
str = "bo"

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

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(lloyd))

def get_class_average(students):
    results = []
    results.append(get_average(students))
    return results.average()


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

students = [lloyd, alice, tyler]


print get_class_average(students)
print get_letter_grade(get_class_average(students))


#2

Here:

def get_class_average(students):
    results = []
    for students in students: # Should be for student in students:
        get_average(students) # Don't need this line
        results.append(get_average(students)) # Should be results.append(get_average(student))
    return average(results)

If you have any questions on why or how this works, please just ask.


#3

Hi @app8,

What is the purpose of this line? ...

str = "bo"

str is the name of a Python built-in function, and the above statement assigns "bo" to that name. Thereafter, the function cannot be called via the name, str.

Repurposing the name of a built-in function often leads to buggy code, and should nearly always be avoided.

See Built-in Functions for a list of Python 2 built-in functions.


#4

Why is get_class_average defined twice? Mine isn't working either.


#5

Technically, one is allowed to redefine a name in a Python program. When a name is used in an expression, the most recent definition of that name is the one that is in effect. However redefining a function is likely to lead to confusing and buggy code, and should be avoided.

In the case of the original post, the first definition of the get_class_average function computes an incorrect result, and should be removed. The second one contains this extra line ...

        get_average(students)

... which contributes nothing to the computation of the result, and that line should be removed.

You are welcome to post your code, so we can look at it.


#6

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