Student becomes the teacher


#1



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


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


apparently my code doesn't calculate the class average correctly and I dont know why


get_letter_grade(get_average(lloyd))
print get_letter_grade

class1 = [lloyd, alice, tyler]

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


#2

a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the function needs to end, in order for this to work, the loop will break

so your loop breaks after calculating the average grade for the first student in the list, the second and or third student are never reached


#3

ok so actually that was my logic the first time I got the error, so I changed the indentation of my return so that it was the end of the function, but I got this error
Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected
with this code
get_letter_grade(get_average(lloyd))
print get_letter_grade

class1 = [lloyd, alice, tyler]

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


#4

you shouldn't loop over class1, your get class average function should work for a class of any size (1, 2 or 3 students)


#5

sorry stetim94 I didnĀ“t really get that, I mean, if I don't loop over a list then I don't need a for loop? Is that what you mean?


#6

no, i am telling you shouldn't specificly loop over class1, if we add this function calls:

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

your function should return the correct total for all 3, that is currently not happening


#7

Oh! Thank you very much! stetim94 I really appreciate it :smiley:


#8

Why have you used 'students' parameter if you are not using it?
You have created another list 'class1' which is not asked?

I think your code should be this:

def get_class_average(students):
results = []
for student in students:
c = get_average(student)
results.append(c)
return average(results)
But I am still getting the same error u got. If u have found the correct code then plz upload it.


#9

a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the loop needs to break otherwise the function can't end.

So your loop ends in its first iteration, so it will never calculate the correct class average for a class with more then one student, how do you think we can fix this?

amitkumar505, that was mainagod mistake, judging by his last reply, he solved it :slight_smile:


#10

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