8.Part of the Whole


#1

I really have this problem for quite a while now and i really need you guys to explain this to me.

Although i found the answer,can someone explain to me how does "student" or "students" automatically refer to the dictionaries tyler,alice,lloyd? and if they don't, can someone explain to me how does it work then?( i mean how the console is going to use the dictionaries)

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]
}

def average(numbers):
total=sum(numbers)
total=float(total)
total/=len(numbers)
return total

def get_average(student):
homework=0.1*average(student["homework"])
quizzes=0.3*average(student["quizzes"])
tests=0.6*average(student["tests"])
return homework+quizzes+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"
get_average(lloyd)
print get_letter_grade(get_average(lloyd))

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


#2

Yeah, the console needs to see a list called students which you haven't created. You only created dictionaries. And you looped for in students here without creating the list.

Well, you create the list like this:

students = [lloyd, alice, tyler]

You either put the list just after the dictionaries you created or after the last function you created that depends on it.

Hope this helps! :slight_smile:


#3

You don't have to create anything. Delete your previous print statement and it will work.
Here is the working code to compare for you:
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]
}

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total/len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    suma = homework * 0.1 + quizzes * 0.3 + tests * 0.6
    return suma
    
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"
    
def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)

#4

Is that?

Where then is your students list your are looping here?


#5

You guys got it wrong ... the way it is now it's completely right it doesn't say i have a mistake but i dont get how my loop ISN'T wrong


#6

Hi, @bamboumis , and everyone else.

It is important to format code when you post it. That enables users to see your indentation and other important details.

See How do I format code in my posts?.

@bamboumis , following is your function definition, with indentation, and it is correct, even if students has not yet been defined in your code...

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

The function definition does not actually give its parameter, students, a value. In fact, students is just a name that does not refer to anything until the function is used, or called. When you actually call the function, you will provide the parameter, students, with a definition by specifying an argument in parentheses, which needs to be a reference to an appropriate list. At that time the function's code, including the loop, will execute.

In Exercise 3: Put It Together, you were asked to do this ...

Below your code, create a list called students that contains lloyd, alice, and tyler.

So, you should have this statement in your code, just below tyler's dictionary ...

students = [lloyd, alice, tyler]

It is important to note that this does not assign a value to the function parameter that is also named students. The global variable, students, created by the above assignment statement is not the same variable as the function parameter, even though they both have the same name.

Assuming that you have defined the global variable, students, you could call the function, as follows ...

print get_class_average(students)

Note that you could also do something like this to have the function provide results for a different group of students ...

coding_club = [alice, tyler]
print get_class_average(coding_club)

It does not matter that the name of the argument in the function call is different from the name of the parameter in the function definition. The above example underscores the fact that the function parameter is given its reference to an object at the time that the function is called. That object, regardless of what it is named in the function call, is referred to as students within the function.


#7

Wow,that was really helpful!Thanks a lot !!!!!


#8

Codecademy is not following that list at that stage. It uses its own list not your students, so if you will copy my code and paste it into the thread I am writing about it will pass and let you go.
Normally I have to write the students list like you mentioned.


#9

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