First of all, assuming grades
is a list containing the grades, you would want to iterate over the grades
list, and not iterate over range(0,50)
.
Second, in every iteration you can use a variable to count how many grades you have seen so far, and another variable that sums all the grades so far. Something like that:
num_grades = 0
sum_grades = 0
for grade in grades:
num_grades += 1 # this is the same as writing num_grades = num_grades + 1
sum_grades += sum # same as writing sum_grades = sum_grades + sum
Now all you need to do is to divide sum_grades
by num_grades
to get the result.
average = float(sum_grade)s / max(num_grades,1)
I used the max
function that returns the maximum number between num_grades and 1 - in case the list of grades is empty, num_grades will be 0 and division by 0 is undefined.
I used float
to get a fraction.
To count the number of grades lower than 50, you can add another variable num_failed
and initialize him to 0
just like num_counts, add an if that check if grade is lower than 50 and if so increase num_failed
by 1.