ZeroDivisionError: float division (baby steps)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Artemisio

    ZeroDivisionError: float division (baby steps)

    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

    "ZeroDivisionEr ror: float division"

    Thank you in advance, Len
  • Diez B. Roggisch

    #2
    Re: ZeroDivisionErr or: float division (baby steps)

    I reduced the code to the lines that actually do something with count:
    [color=blue]
    > count= 0
    > count= count + 1
    > count= count -1[/color]

    This yields count beeing 0 - thus you get a ZeroDivisionErr or, 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

    Comment

    • Benjamin Niemann

      #3
      Re: ZeroDivisionErr or: float division (baby steps)

      Artemisio wrote:[color=blue]
      > 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
      >
      > "ZeroDivisionEr ror: float division"
      >
      > Thank you in advance, Len[/color]
      I think the two lines after input() should be indented as the belong to
      the while loop...

      Comment

      • Russell Blau

        #4
        Re: ZeroDivisionErr or: float division (baby steps)

        "Benjamin Niemann" <pink@odahoda.d e> wrote in message
        news:cg3184$vbr $02$1@news.t-online.com...[color=blue]
        > Artemisio wrote:[color=green]
        > > 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[/color][/color]
        exercise:[color=blue][color=green]
        > >
        > > 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
        > >
        > > "ZeroDivisionEr ror: float division"
        > >
        > > Thank you in advance, Len[/color]
        > I think the two lines after input() should be indented as the belong to
        > the while loop...[/color]

        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.


        Comment

        • Artemisio

          #5
          Re: ZeroDivisionErr or: float division (baby steps)

          "Russell Blau" <russblau@hotma il.com> wrote in message news:<2okg05Fbh 7kjU1@uni-berlin.de>...[color=blue]
          > "Benjamin Niemann" <pink@odahoda.d e> wrote in message
          > news:cg3184$vbr $02$1@news.t-online.com...[color=green]
          > > Artemisio wrote:[color=darkred]
          > > > 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[/color][/color]
          > exercise:[color=green][color=darkred]
          > > >
          > > > 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
          > > >
          > > > "ZeroDivisionEr ror: float division"
          > > >
          > > > Thank you in advance, Len[/color]
          > > I think the two lines after input() should be indented as the belong to
          > > the while loop...[/color]
          >
          > Note that, even after you fix this, you will still get an error if the user
          > decides to enter "0" as the first number![/color]

          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.

          Comment

          • Dan Bishop

            #6
            Re: ZeroDivisionErr or: float division (baby steps)

            calidusdk@hotma il.com (Artemisio) wrote in message news:<6daa8765. 0408191133.2f2e 22e3@posting.go ogle.com>...[color=blue]
            > 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[/color]

            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
            [color=blue]
            > sum= 0.0[/color]

            because you'll get the same division results from "sum=0". (If you
            really want integer division, use the // operator.)
            [color=blue]
            > 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[/color]

            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].verboseAttribu te += 1
            [color=blue]
            > print "The average is: ", sum / count
            > #the error is in the above line[/color]

            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

            Comment

            • Artemisio

              #7
              Re: ZeroDivisionErr or: float division (baby steps)

              danb_83@yahoo.c om (Dan Bishop) wrote in message news:<ad052e5c. 0408201930.1123 ec9d@posting.go ogle.com>...[color=blue]
              > calidusdk@hotma il.com (Artemisio) wrote in message news:<6daa8765. 0408191133.2f2e 22e3@posting.go ogle.com>...[color=green]
              > > 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[/color]
              >
              > 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
              >[color=green]
              > > sum= 0.0[/color]
              >
              > because you'll get the same division results from "sum=0". (If you
              > really want integer division, use the // operator.)
              >[color=green]
              > > 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[/color]
              >
              > 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].verboseAttribu te += 1
              >[color=green]
              > > print "The average is: ", sum / count
              > > #the error is in the above line[/color]
              >
              > 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[/color]

              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.

              Comment

              Working...