Understanding something


#1
`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 = 0
    total = float(sum(numbers))
    total /= len(numbers)
    return total
def get_average(student):
    homework = 0
    homework = average(student["homework"])
    quizzes = 0
    quizzes = average(student['quizzes'])
    tests = 0
    tests = average(student['tests'])
    return 0.10 * homework + 0.3 * quizzes + 0.60 * 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):
    students = list(students)
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)
    
print get_class_average(students)
print get_letter_grade(students)

Hello guys, i finished this part of the course but i had to use some of the forum help, and have a doubt hope you can help with:

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

I didn't really understand the students = list(students), making it with the forum help.. The python list function says it iterates all the values in a list, but being students already defined, why do you need to make this?

Thanks

`


#2

Hello :slight_smile:

Nice question! The line:

students = list(students)

is NOT at all required in the get_class_average method.

Such a technique of calling list on an existing variable is only when 1) you want to convert an iterable (like tuples) to a list 2) get a duplicate copy of the list.

Let me explain the need for the second point. Sometimes when you have code like this:

def spooky_method(data):
    data[1] = 5
    data[3] = 6

def main():
    precious_data = [1, 2, 3, 4]
    print(precious_data) # data is safe! :)
    spooky_method(precious_data)
    print(precious_data) # oh no! precious data got modified!

where it modifies the input array, then it is best to clone the input array, like this:

def not_so_spooky_method(data):
    data = list(data) # cloned!
    data[1] = 5
    data[3] = 6

def main():
    precious_data = [1, 2, 3, 4]
    print(precious_data) # data is safe! :)
    not_so_spooky_method(precious_data)
    print(precious_data) # data is still safe! :D

Now, the changes you make in your not_so_spooky_method won't be reflected in the list that the main method has.

But since your get_class_average method is NOT performing any "mutation" (that's the term for what your spooky_method was doing), thus there is no need to perform any cloning at all.

Hope it helps! :slight_smile: