ZeroDivisionError: float division (baby steps)

A

Artemisio

I am a non programmer who just started with Python. So far I love it.

I would appreciate if you could help me fix this error I get taking this exercise:

count= 0
sum= 0.0
number= 1
print "Enter 0 to exit the loop"

while number != 0 :
number= input("Enter a number: ")

count= count + 1
sum= sum + number

count= count -1
print "The average is: ",sum/count
#the error is in the above line

"ZeroDivisionError: float division"

Thank you in advance, Len
 
D

Diez B. Roggisch

I reduced the code to the lines that actually do something with count:
count= 0
count= count + 1
count= count -1

This yields count beeing 0 - thus you get a ZeroDivisionError, as one has to
expect for division by zero....

I'm not totally sure what you actually want, but it seems to me that you
should indent the lines

count= count + 1
sum= sum + number

to the same level as the loop - that will make them part of the loop, so you
actually get some numbers accumulated.
 
B

Benjamin Niemann

Artemisio said:
I am a non programmer who just started with Python. So far I love it.

I would appreciate if you could help me fix this error I get taking this exercise:

count= 0
sum= 0.0
number= 1
print "Enter 0 to exit the loop"

while number != 0 :
number= input("Enter a number: ")

count= count + 1
sum= sum + number

count= count -1
print "The average is: ",sum/count
#the error is in the above line

"ZeroDivisionError: float division"

Thank you in advance, Len
I think the two lines after input() should be indented as the belong to
the while loop...
 
R

Russell Blau

Benjamin Niemann said:
I think the two lines after input() should be indented as the belong to
the while loop...

Note that, even after you fix this, you will still get an error if the user
decides to enter "0" as the first number!
 
A

Artemisio

Russell Blau said:
Note that, even after you fix this, you will still get an error if the user
decides to enter "0" as the first number!

Well, thank you very much for your feedback. I've sorted out now. For
some reason the last two lines were indented. Unindenting them
producted the expected behaviour.

I am actually amazed by how intuitive and readable Python coding is.
I've spent maybe 12 hours with it and I can already code small
routines of my own. I felt more frustrated last time I tried to learn
VB. I gave up. Python is my game.
 
D

Dan Bishop

I am a non programmer who just started with Python. So far I love it.

I would appreciate if you could help me fix this error I get taking this
exercise:

count= 0

As other posters have mentioned, the problem is with your indentation.
But I can't resist giving advice.

First of all, I recommend starting every file with the line "from
__future__ import division". You will then no longer need to worry as
much about writing things like

because you'll get the same division results from "sum=0". (If you
really want integer division, use the // operator.)
number= 1
print "Enter 0 to exit the loop"

while number != 0 :
number= input("Enter a number: ")

count= count + 1 # [indentation corrected]
sum= sum + number # [indentation corrected]

count= count -1

Instead of using sentinel values, it's possible to put the loop
condition in the middle of the loop, like this:

print "Enter 0 to exit the loop"

while True: # loop "forever"
number = input("Enter a number: ")
if number == 0: # condition for exiting the loop
break
count += 1
sum += number

Note that count no longer needs to be decremented by 1 at the end,
because if you enter 0, it doesn't get incremented.

Also note that assignments of the form x=x+y can be abbreviated as
x+=y, so you don't have to write the left-hand side twice. The
benefit will be more noticeable for statements like

verboseName[complicated + index + calculation].verboseAttribute += 1
print "The average is: ", sum / count
#the error is in the above line

Often, the real error is long before the line that gives you the error
message.

But you might want to modify this line to deal with the situation that
count == 0.

if count == 0:
print "You didn't enter any numbers!"
else:
print "The average is: ", sum / count
 
A

Artemisio

I am a non programmer who just started with Python. So far I love it.

I would appreciate if you could help me fix this error I get taking this
exercise:

count= 0

As other posters have mentioned, the problem is with your indentation.
But I can't resist giving advice.

First of all, I recommend starting every file with the line "from
__future__ import division". You will then no longer need to worry as
much about writing things like

because you'll get the same division results from "sum=0". (If you
really want integer division, use the // operator.)
number= 1
print "Enter 0 to exit the loop"

while number != 0 :
number= input("Enter a number: ")

count= count + 1 # [indentation corrected]
sum= sum + number # [indentation corrected]

count= count -1

Instead of using sentinel values, it's possible to put the loop
condition in the middle of the loop, like this:

print "Enter 0 to exit the loop"

while True: # loop "forever"
number = input("Enter a number: ")
if number == 0: # condition for exiting the loop
break
count += 1
sum += number

Note that count no longer needs to be decremented by 1 at the end,
because if you enter 0, it doesn't get incremented.

Also note that assignments of the form x=x+y can be abbreviated as
x+=y, so you don't have to write the left-hand side twice. The
benefit will be more noticeable for statements like

verboseName[complicated + index + calculation].verboseAttribute += 1
print "The average is: ", sum / count
#the error is in the above line

Often, the real error is long before the line that gives you the error
message.

But you might want to modify this line to deal with the situation that
count == 0.

if count == 0:
print "You didn't enter any numbers!"
else:
print "The average is: ", sum / count

Thank you very much, Dan!
When one is a total beginner every bit of advice is most welcome. As I
write I am having a closer look to your tips.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top