Need Help with the Loops

Hi,

I am new to coding and following the topics in Python course here at Codecademy. I am currently studying the loops and thought I’d take my time outside the course to practice them a little before I go on further in the course. I have written the code below but cannot figure out why a certain part of it is not working. I’d appreciate it if someone points me in the right direction.

I have a list of grades stored in the variable grades. There are 10 grades in this variable. I am trying to print the grades that are above the average of the grades in the list. Once the average is calculated, it is stored in a variable called average and the average is 48.5. To make sure the calculations are correct, I have printed them at the bottom of the code and calculated them manually as well. But when it comes to printing the grades above the average, I cannot get the if statement to work. It is taking out some of the numbers like 12 and 24 like it should, but still keeping other grades lower than average like 24, 28 and 47.

Here is the code:

grades = [62, 75, 33, 24, 28, 56, 88, 12, 47, 60]

sum_grades = 0

for grade in grades:
    
    sum_grades += grade
    length = len(grades)
    average = sum_grades / length

    if grade > average:
        print(grade)

print("The total is " + str(sum_grades) + ".")
print("The average is " + str(average) + ".")
print("The list has " + str(len(grades)) + " numbers.")

And here is the result:

62
75
33
24
28
56
88
47
60
The total is 485.
The average is 48.5.
The list has 10 numbers.

I am not sure if I am doing something that shouldn’t be done in a loop or if this is related to something I haven’t learned yet. I tried searching for a possible solution on Google but couldn’t find anything useful.

Thanks in advance for the help! If you notice something in the code that is not considered best practice, please feel free to point them out.

an average is calculated by taking the sum, then divide the sum by the length

you do this as well, but every iterations of the loop. I would recommended your code here:

http://www.pythontutor.com/visualize.html#mode=edit

then you can step through it.

so in the first iteration of the loop, sum_grades becomes 62. Then you divide 62 by 10, so your average is now 6.2. 62 > 6.2 is true

then the next iteration of the loop, sum_grades becomes 137. 137 / 10 = 13.7. And so forth

you shouldn’t re-calculate the length of the list each iteration, why would you do this?

1 Like

Hi stetim94,

Thank you for the answer! If I understood correctly, I should be doing the calculations outside the loop, right? So maybe it seems I chose a wrong example to practice loops.

By the way, thank you for link. I did not know such a tool existed for visualizing the code.

once you move outside codecademy (if you haven’t already), you should see that your text-editor/IDE of choice supports a debugger. Really makes life easier

the loop is useful to get the sum. But then after/outside the loop you should do the division/calculate the average

no, I think its good example. You practice how to use loop, which logic should go inside the loop (and what not)

1 Like

Thank you very much for your help, I appreciate it!.

1 Like