Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grade Loop
#1
Hello, my assignment is to make a program that allows a teacher to enter any number of tests grades and it will give them the average. My problem is my average isn't correctly working? For example I plug in numbers 85, 75, 96 I SHOULD get 85.3. However I'm getting in my return, 86.2. Anyone able to tell me where my math is wrong or what would be causing this to spit out a number 1.1 off?

My code:
count = 0
total = 0
toContinue = "y"
while toContinue != "n":
    grade = int(input("Enter a test grade: "))
    for eachPass in range(grade):
        count = count + 1
        total = total + grade
    toContinue = input ("Are there any more test grades to enter, 'y' or 'n'? ")
    if toContinue == "n":
        print("The average is", round(total / count, 1))
        print("I'm done")
        break
Reply
#2
Run this
 
count = 0
total = 0
toContinue = "y"
while toContinue != "n":
    grade = int(input("Enter a test grade: "))
    for eachPass in range(grade):
        count = count + 1
        total = total + grade
        print('eachPass: {}, total: {}'.format(eachPass, total))
    toContinue = input ("Are there any more test grades to enter, 'y' or 'n'? ")
    if toContinue == "n":
        print('Final total: {}, Final count: {}'.format(total, count))
        print("The average is", round(total / count, 1))
        print("I'm done")
        break
Does it make more sense now, why you get incorrect average? :-)
Reply
#3
(Oct-17-2017, 01:35 PM)buran Wrote: Run this
 
count = 0
total = 0
toContinue = "y"
while toContinue != "n":
    grade = int(input("Enter a test grade: "))
    for eachPass in range(grade):
        count = count + 1
        total = total + grade
        print('eachPass: {}, total: {}'.format(eachPass, total))
    toContinue = input ("Are there any more test grades to enter, 'y' or 'n'? ")
    if toContinue == "n":
        print('Final total: {}, Final count: {}'.format(total, count))
        print("The average is", round(total / count, 1))
        print("I'm done")
        break
Does it make more sense now, why you get incorrect average? :-)
I'm a total noob here to python (taking classes on it), would you care to explain just a tiny bit more?
Reply
#4
did you run it? I just added 2 print functions to show you what is going on in your code.
Reply
#5
(Oct-17-2017, 01:44 PM)buran Wrote: did you run it? I just added 2 print functions to show you what is going on in your code.

Yeah, the grade number is multiplying its self. So, should I change the "range"?
Reply
#6
you should ask yourself 'Do I need that range loop?'
Reply
#7
:D I see what I did there. I got it working. Thanks!!!
Reply
#8
Please, don't PM. If you removed the loop (line#6) and unindent lines#7 and#8 one level your code should work
Reply
#9
If you fix it - why don't you refactor your code(i.e. change it) to avoid asking user each time is there another grade... This will make user experience much better
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Method to calculate average and grade ajitnayak1987 8 6,232 Apr-28-2022, 06:26 AM
Last Post: rayansaqer
  Non Grade/School Help - PLEASE gbyrne12 8 3,027 Jun-19-2021, 07:31 PM
Last Post: snippsat
  Calculating Grade Average IstvanCH 5 5,018 Jan-27-2019, 04:42 PM
Last Post: aakashjha001
  Student grade program help debug ccm1776 3 5,095 Nov-14-2018, 02:41 AM
Last Post: stullis
  Help? Letter Grade assignment.. zepel 3 4,581 Apr-23-2017, 12:47 PM
Last Post: idontreallywolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020