[Tutor] Help

Alan Gauld alan.gauld at yahoo.co.uk
Wed Oct 7 04:16:17 EDT 2020


On 07/10/2020 08:01, Donovan Fitch wrote:
> totalNum = int(input("Please enter the total number of students: "))
> student = 1
> print("Grades should be between 0 and 100.")
> while student <= totalNum:
>     grade = int(input("Student {} grade: ".format(student)))
>     student += 1
> 
>     for i in range(1, totalNum, 1):
>         if grade > max:
>             max = grade
>         if grade < min:
>             min = grade
> 
> print("Maximum grade: ", max)
> print("Minimum grade: ", min)
> 
> It works perfectly with finding the max and min, however, I need to know
> how to find the average of the grades.

So how would you work out any average?
Find the total and divide by the number of items.
So you need to either

a) store all the grades in a list and sum() them at the end.  Then
divide by the len() of the list.

or

b) keep a running total and count and do the math at the end.

BTW Your for loop is completely unnecessary. It just
performs the exact same operation many times.
grade never changes so max/min get set to the
same value each time.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list