ZeroDivisionError: float division (baby steps)

Artemisio calidusdk at hotmail.com
Sat Aug 21 13:55:40 EDT 2004


danb_83 at yahoo.com (Dan Bishop) wrote in message news:<ad052e5c.0408201930.1123ec9d at posting.google.com>...
> calidusdk at hotmail.com (Artemisio) wrote in message news:<6daa8765.0408191133.2f2e22e3 at posting.google.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.



More information about the Python-list mailing list