# ZeroDivisionError: float division (baby steps)

Discussion in 'Python' started by Artemisio, Aug 19, 2004.

1. ### ArtemisioGuest

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"

Artemisio, Aug 19, 2004

2. ### Diez B. RoggischGuest

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.

--
Regards,

Diez B. Roggisch

Diez B. Roggisch, Aug 19, 2004

3. ### Benjamin NiemannGuest

Artemisio wrote:
> 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...

Benjamin Niemann, Aug 19, 2004
4. ### Russell BlauGuest

"Benjamin Niemann" <> wrote in message
news:cg3184\$vbr\$02\$-online.com...
> Artemisio wrote:
> > 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...

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

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.

Russell Blau, Aug 19, 2004
5. ### ArtemisioGuest

"Russell Blau" <> wrote in message news:<>...
> "Benjamin Niemann" <> wrote in message
> news:cg3184\$vbr\$02\$-online.com...
> > Artemisio wrote:
> > > 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...

>
> 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.

Artemisio, Aug 20, 2004
6. ### Dan BishopGuest

(Artemisio) wrote in message news:<>...
> 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

> sum= 0.0

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

Dan Bishop, Aug 21, 2004
7. ### ArtemisioGuest

(Dan Bishop) wrote in message news:<>...
> (Artemisio) wrote in message news:<>...
> > 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
>
> > sum= 0.0

>
> 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.

Artemisio, Aug 21, 2004