Joseph Yeagle Joseph Yeagle - 20 days ago 6
Python Question

Not getting the average

count = 0
total = 0
while True:

inp = raw_input ('enter a number:')
if inp == 'done' : break
if len(inp) < 1 : break


num = float(inp)
count = count + 1
total = total + num
print num, total, count

print "average:", total/count


My print out is not showing the average, Am I missing something?

jez jez
Answer

I expect you're forgetting to enter the string done once you've finished feeding it numbers. Alternatively you can just press return, giving it an empty input. As written, the code is only designed to give you the average after you've done one of those two things.

If you want it to show the running average after each input, indent the last line of code (the one that prints the average) so that it's part of the loop.

Comments