Part of whole


#1



Im struck in this part of whole


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 =float(sum(numbers))
    result = float(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):
    if score >= 90:
        return "A"
    if score >= 80:
        return "B"
    if score >= 70:
        return "C"
    if score >= 60:
        return "D"
    else:
        return "F"
        
def get_class_average(students):
    students = ["Tyler", "Alice", "Lloyd"];
        
    
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)

ERROR : Oops, try again.
** get_class_average([alice]) resulted in an error: string indices must be integers, not str**


#2

in your get_class_average, this line:

students = ["Tyler", "Alice", "Lloyd"];

why? (and why the semi-colon, this is python, not js), but why for the whole line? You can pass the students name as argument when calling the function, no need to overwrite your function parameter


#3

@stetim94 Just curious, but if you choose to use this list instead of just passing their names as arguments when calling the function, you have to shift the list out of the function in order for it to work right? If so, why must it be outside of the function in order for it to work? Thanks :slight_smile:


#4

yes, the list should be outside the function (in the function call), or create a variable which holds the list, and pass the variable into the function call, both methods work, but the list should definitely outside the function


#5

For the Student Becomes the Teacher course, we want to write functions that have general usefulness. What this means is that, although we are working with a specific list of three students in these exercises, the functions that we write should be able to work with any list of students, provided that the data for the students conforms to a specified format. This semester, we have three students; next semester, we might have four of them, with different names and grades. Once we have perfected our functions, we want to be able to use them again without having to rewrite them.

More generally, when programmers write functions, they want them to be as useful as possible. Functions are intended to be re-useable pieces of code. Once they have been tested and debugged, the authors of the functions can use them to perform specified types of tasks repeatedly, as well as distribute them to other users to enhance those users' productivity.

That is why this list, which we write in Exercise 3: Put It Together, should be defined outside the function ...

students = [lloyd, alice, tyler]

If we do it that way, we can pass this, or any other list of students that we define, to the function by specifying the name of the list as an argument.

This semester, we have ...

students = [lloyd, alice, tyler]
print get_class_average(students)

Next semester, we might have ...

students_fall_2016 = [jack, jill, zack, chloe]
print get_class_average(students_fall_2016)

Note that the list should be a list of student dictionaries, and not a list of strings. Also note that the name of the list that is passed as an argument to the function, when it is called, does not need to be the same as the name of the list that is specified in the function definition header.


#6

Thank you very much for the detailed response :slight_smile:

Just want to check one last thing. You said that:

By this, do you mean like for example:

def function_name(list_name1):
(function body with correct indentation)
return ____

function_name(list_name2)

So you mean this will work as well right? :slight_smile:


#7

Hi, @umopapsidn ,

Just to clarify with an example ...

def combine_and_sort(list_a, list_b):
    # combine and sort two lists; return the result
    new_list = list_a + list_b
    new_list.sort()
    return new_list

m = [1, 7, 3, 8]
n = [4, 8, 3, 9]
o = [6, 2, 5, 4]

print combine_and_sort(m, n)
print combine_and_sort(n, o)
print combine_and_sort(m, o)
print combine_and_sort(m, combine_and_sort(n, o))

Output:

[1, 3, 3, 4, 7, 8, 8, 9]
[2, 3, 4, 4, 5, 6, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 3, 4, 4, 5, 6, 7, 8, 8, 9]

The function needs to be defined only once, then we can call it multiple times with different arguments.