Posts: 16
Threads: 3
Joined: Oct 2017
Oct-17-2017, 01:28 PM
(This post was last modified: Oct-17-2017, 01:29 PM by dtweaponx.)
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
Posts: 8,160
Threads: 160
Joined: Sep 2016
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? :-)
Posts: 16
Threads: 3
Joined: Oct 2017
(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?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Oct-17-2017, 01:44 PM
(This post was last modified: Oct-17-2017, 01:45 PM by buran.)
did you run it? I just added 2 print functions to show you what is going on in your code.
Posts: 16
Threads: 3
Joined: Oct 2017
(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"?
Posts: 8,160
Threads: 160
Joined: Sep 2016
you should ask yourself 'Do I need that range loop?'
Posts: 16
Threads: 3
Joined: Oct 2017
:D I see what I did there. I got it working. Thanks!!!
Posts: 8,160
Threads: 160
Joined: Sep 2016
Please, don't PM. If you removed the loop (line#6) and unindent lines#7 and#8 one level your code should work
Posts: 8,160
Threads: 160
Joined: Sep 2016
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
|