Velocity Reviews > ZeroDivisionError: float division (baby steps)

# ZeroDivisionError: float division (baby steps)

Artemisio
Guest
Posts: n/a

 08-19-2004
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"

Diez B. Roggisch
Guest
Posts: n/a

 08-19-2004
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

Benjamin Niemann
Guest
Posts: n/a

 08-19-2004
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...

Russell Blau
Guest
Posts: n/a

 08-19-2004
"Benjamin Niemann" <(E-Mail Removed)> wrote in message
news:cg3184\$vbr\$02\$(E-Mail Removed)-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.

Artemisio
Guest
Posts: n/a

 08-20-2004
"Russell Blau" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "Benjamin Niemann" <(E-Mail Removed)> wrote in message
> news:cg3184\$vbr\$02\$(E-Mail Removed)-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.

Dan Bishop
Guest
Posts: n/a

 08-21-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Artemisio) wrote in message news:<(E-Mail Removed). com>...
> 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

Artemisio
Guest
Posts: n/a

 08-21-2004
(E-Mail Removed) (Dan Bishop) wrote in message news:<(E-Mail Removed). com>...
> (E-Mail Removed) (Artemisio) wrote in message news:<(E-Mail Removed). com>...
> > 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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Carsten Fuchs C++ 45 10-08-2009 09:47 AM krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM Timothy Fitz Python 0 10-27-2004 05:37 PM Razvan Java 77 10-19-2004 03:42 AM bd C Programming 0 07-07-2003 12:09 AM