0

I have to create a program that shows the arithmetic mean of a list of variables. There are supposed to be 50 grades.

I'm pretty much stuck. Right now I´ve only got:

for c in range (0,50):
    grade = ("What is the grade?")

Also, how could I print the count of grades that are below 50?

Any help is appreciated.

paisanco
  • 4,098
  • 6
  • 27
  • 33

4 Answers4

2

If you don't mind using numpy this is ridiculously easy:

import numpy as np
print np.mean(grades)

Or if you'd rather not import anything,

print float(sum(grades))/len(grades)

To get the number of grades below 50, assuming you have them all in a list, you could do:

grades2 = [x for x in grades if x < 50]
print len(grades2)
user3543300
  • 499
  • 2
  • 9
  • 27
1

Assuming you have a list with all the grades.

avg = sum(gradeList)/len(gradeList)

This is actually faster than numpy.mean().

To find the number of grades less than 50 you can put it in a loop with a conditional statement.

numPoorGrades = 0
for g in grades:
    if g < 50:
        numPoorGrades += 1    

You could also write this a little more compactly using a list comprehension.

numPoorGrades = len([g for g in grades if g < 50])
eeScott
  • 143
  • 9
0

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.

galfisher
  • 1,122
  • 1
  • 13
  • 24
0

Try the following. Function isNumber tries to convert the input, which is read as a string, to a float, which I believe convers the integer range too and is the floating-point type in Python 3, which is the version I'm using. The try...except block is similar in a way to the try...catch statement found in other programming languages.

#Checks whether the value is a valid number:
def isNumber( value ):
    try:
        float( value )
        return True
    except:
        return False

#Variables initialization:
numberOfGradesBelow50 = 0
sumOfAllGrades = 0

#Input:
for c in range( 0, 5 ):
    currentGradeAsString = input( "What is the grade? " )
    while not isNumber( currentGradeAsString ):
        currentGradeAsString = input( "Invalid value. What is the grade? " )
    currentGradeAsFloat = float( currentGradeAsString )
    sumOfAllGrades += currentGradeAsFloat
    if currentGradeAsFloat < 50.0:
        numberOfGradesBelow50 += 1

#Displays results:
print( "The average is " + str( sumOfAllGrades / 5 ) + "." )
print( "You entered " + str( numberOfGradesBelow50 ) + " grades below 50." )