8.Part of the Whole


#1




Oops, try again. get_class_average([alice]) returned 80.55 instead of 91.15 as expected

whats wrong with my code?

Replace this line with your 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)
        total = total/len(numbers)
        return total
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"
    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=[lloyd,alice,tyler]
    results=[]
    for student in students:
        results.append(get_average(student))
        return average(results)


#2

Hi @shrijan23 ,

When a return statement is placed within a for loop, how does it affect the execution of the loop, and what effect does that have on the result in the current case?


#3

hey
when a return statement is placed within a for loop, I think the execution ends there itself and the value is returned. is it correct?


#4

This is correct, because return ends a function. What way can you make sure that you return after the for loop is completed?


#5

may be i can put 'break' before return. will that work?


#6

This would not work, but I think you're over thinking the solution.
Python focuses on indentation, so if something is indented further than another line, then it is typically considered 'inside of it'. What you can do is simply unindent the line that holds your return statement, so that it is right after your for loop. Does this make sense? I hope this helped, I wanted to explain it without giving you the entire answer.


#7

haha Yes I have tried to unindent it and it still throws an error.

error- Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected

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)
total = total/len(numbers)
return total
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"
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=[lloyd,alice,tyler]
results=[]
for student in students:
results.append(get_average(student))
return average(results)

Do you think there is something wrong with the code?


#8


#9

@shrijan23 ,

What is the purpose of placing this statement inside your get_class_average function? ...

students=[lloyd,alice,tyler]

#10

You do not need to create a new list for the students. It is said that you can expect it so it is not needed but I guess it doesnt hurt to have it anyway. It is just unnecessary


#13

well you know what, i removed this line - students=[lloyd,alice,tyler] and it actually worked.
now i wonder how this line was affecting the program?


#14

@shrijan23 ,

Following are the first two lines of the get_class_average that you originally posted ...

def get_class_average(students):
    students=[lloyd,alice,tyler]

The purpose of the students parameter specified in the function header is to enable anyone who calls the function to pass a list of student dictionaries to it as an argument. That list might consist of lloyd, alice, and tyler, or it could be a list of other student dictionaries. Based on the name of the parameter, that list will be referred by the name, students, within the function.

The problem with your original code was that the next line after the function header assigns [lloyd,alice,tyler] to students. Therefore, if Codecademy or any other user of the function passes a different list of student dictionaries to the function when calling it, the function computes results for lloyd, alice, and tyler, regardless of who is in the actual list that was passed to it. That is what happened when Codecademy tested your function by passing it a list with only alice in it. The result should have been 91.15, but the inclusion of lloyd and tyler brought the result down to 80.55.


#15