how to finish a while loop...

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

    how to finish a while loop...

    Hi all, i'm new to python. Learning on my own how to ask a user to
    finish a loop or not.
    For some reason, it behaves as infinite loop although I changed its
    condition. Please tell me what I'm doing wrong. Thanks in advance.


    condition = True

    while ( condition ):

    try:
    integer_one = int ( raw_input( "Please enter an
    integer: " ) )
    integer_two = int ( raw_input( "Please enter the
    second integer: " ) )
    division = integer_one / integer_two

    except( ZeroDivisionErr or ):
    print "\nDivision by zero detected"
    except( ValueError ):
    print "\nYou didn't enter an integer"
    else:
    print "The result is", division
    answer = raw_input("Do you want to try again (yes or
    no)? ")
    if answer == 'yes':
    condition
    elif answer == 'no':
    condition = False

    print "Good bye, you don't want to continue"
  • Guilherme Polo

    #2
    Re: how to finish a while loop...

    2008/2/19, icarus <rsarpi@gmail.c om>:
    Hi all, i'm new to python. Learning on my own how to ask a user to
    finish a loop or not.
    For some reason, it behaves as infinite loop although I changed its
    condition. Please tell me what I'm doing wrong. Thanks in advance.
    >
    >
    condition = True
    >
    while ( condition ):
    >
    try:
    integer_one = int ( raw_input( "Please enter an
    integer: " ) )
    integer_two = int ( raw_input( "Please enter the
    second integer: " ) )
    division = integer_one / integer_two
    >
    except( ZeroDivisionErr or ):
    print "\nDivision by zero detected"
    except( ValueError ):
    print "\nYou didn't enter an integer"
    else:
    print "The result is", division
    answer = raw_input("Do you want to try again (yes or
    no)? ")
    if answer == 'yes':
    condition
    elif answer == 'no':
    condition = False
    >
    print "Good bye, you don't want to continue"
    >
    --

    >
    That code works. Maybe you fixed it while you were mailing it =)

    --
    -- Guilherme H. Polo Goncalves

    Comment

    • richie

      #3
      Re: how to finish a while loop...

      On Feb 20, 9:00 am, icarus <rsa...@gmail.c omwrote:
      Hi all, i'm new to python. Learning on my own how to ask a user to
      finish a loop or not.
      For some reason, it behaves as infinite loop although I changed its
      condition. Please tell me what I'm doing wrong. Thanks in advance.
      >
      condition = True
      >
      while ( condition ):
      >
      try:
      integer_one = int ( raw_input( "Please enter an
      integer: " ) )
      integer_two = int ( raw_input( "Please enter the
      second integer: " ) )
      division = integer_one / integer_two
      >
      except( ZeroDivisionErr or ):
      print "\nDivision by zero detected"
      except( ValueError ):
      print "\nYou didn't enter an integer"
      else:
      print "The result is", division
      answer = raw_input("Do you want to try again (yes or
      no)? ")
      if answer == 'yes':
      condition
      elif answer == 'no':
      condition = False
      >
      print "Good bye, you don't want to continue"
      condition = True

      while ( condition ):
      try:
      integer_one = int ( raw_input( "Please enter an integer: " ) )
      integer_two = int ( raw_input( "Please enter the second
      integer: " ) )
      division = integer_one / integer_two
      except( ZeroDivisionErr or ):
      print "\nDivision by zero detected"
      except( ValueError ):
      print "\nYou didn't enter an integer"
      else:
      print "The result is", division
      answer = raw_input("Do you want to try again (yes or no)? ")
      if answer == 'yes':
      condition
      elif answer == 'no':
      condition = False
      print "Good bye, you don't want to continue"
      Try this.
      The indent is very important in python. Take more care about it.
      You'll find python is very good for us.

      Comment

      • Paul Rubin

        #4
        Re: how to finish a while loop...

        icarus <rsarpi@gmail.c omwrites:
        For some reason, it behaves as infinite loop although I changed its
        condition. Please tell me what I'm doing wrong. Thanks in advance.
        It worked when I tried it:
        >>## working on region in file /usr/tmp/python-13922e2f...
        Please enter an integer: 8
        Please enter the second integer: 3
        The result is 2
        Do you want to try again (yes or no)? yes
        Please enter an integer: 8
        Please enter the second integer: 2
        The result is 4
        Do you want to try again (yes or no)? no
        Good bye, you don't want to continue

        Comment

        • icarus

          #5
          Re: how to finish a while loop...

          That code works. Maybe you fixed it while you were mailing it =)
          >
          --
          -- Guilherme H. Polo Goncalves

          This is weird mate.
          I'm using eclipse 3.2 with the pydev plugin. There it loops forever -
          from the eclipse console.
          Two hours of trying, changing the code...finally gave up.

          Then I got your reply. Opened up a regular console and executed it
          from there.
          And voila....it works! Well, after this I'm going back to the old
          trusty shell.

          Thanks again mate.

          Comment

          • Preston  Landers

            #6
            Re: how to finish a while loop...

            On Feb 19, 7:12 pm, richie <richie...@gmai l.comwrote:
            On Feb 20, 9:00 am, icarus <rsa...@gmail.c omwrote:
            >
            >
            >
            Hi all, i'm new to python. Learning on my own how to ask a user to
            finish a loop or not.
            For some reason, it behaves as infinite loop although I changed its
            condition. Please tell me what I'm doing wrong. Thanks in advance.
            >
            condition = True
            >
            while ( condition ):
            >
            try:
            integer_one = int ( raw_input( "Please enter an
            integer: " ) )
            integer_two = int ( raw_input( "Please enter the
            second integer: " ) )
            division = integer_one / integer_two
            >
            except( ZeroDivisionErr or ):
            print "\nDivision by zero detected"
            except( ValueError ):
            print "\nYou didn't enter an integer"
            else:
            print "The result is", division
            answer = raw_input("Do you want to try again (yes or
            no)? ")
            if answer == 'yes':
            condition
            elif answer == 'no':
            condition = False
            >
            print "Good bye, you don't want to continue"
            >
            condition = True
            >
            while ( condition ):
            try:
            integer_one = int ( raw_input( "Please enter an integer: " ) )
            integer_two = int ( raw_input( "Please enter the second
            integer: " ) )
            division = integer_one / integer_two
            except( ZeroDivisionErr or ):
            print "\nDivision by zero detected"
            except( ValueError ):
            print "\nYou didn't enter an integer"
            else:
            print "The result is", division
            answer = raw_input("Do you want to try again (yes or no)? ")
            if answer == 'yes':
            condition
            elif answer == 'no':
            condition = False
            print "Good bye, you don't want to continue"
            Try this.
            The indent is very important in python. Take more care about it.
            You'll find python is very good for us.

            Wouldn't your version get a NameError on the "if answer == 'yes'" line
            in the case of a ZeroDivisionErr or or ValueError?

            To the original poster.... what environment are you running this in?
            When I put your program in notepad and run it from the windows command
            prompt it works. But when I paste it into eclipse and run it
            eclipse's console, it doesn't work because answer seems to have a
            stray '\r' carriage return (CR) and therefore the comparison to 'no'
            fails.

            Comment

            • richie

              #7
              Re: how to finish a while loop...

              On Feb 20, 9:21 am, icarus <rsa...@gmail.c omwrote:
              That code works. Maybe you fixed it while you were mailing it =)
              >
              --
              -- Guilherme H. Polo Goncalves
              >
              This is weird mate.
              I'm using eclipse 3.2 with the pydev plugin. There it loops forever -
              from the eclipse console.
              Two hours of trying, changing the code...finally gave up.
              >
              Then I got your reply. Opened up a regular console and executed it
              from there.
              And voila....it works! Well, after this I'm going back to the old
              trusty shell.
              >
              Thanks again mate.
              I try it too in my eclipse3.2. I got the same result.
              It seems very strange.

              Comment

              • icarus

                #8
                Re: how to finish a while loop...

                To the original poster.... what environment are you running this in?
                Linux. Xubuntu if that matters.
                When I put your program in notepad and run it from the windows command
                prompt it works.
                yeah yeah...same here.
                After I got the tip that it actually worked, I went into the eclipse
                directory where the program lives, ran it there from the shell, and it
                worked. Meaning, I didn't modify anything on the file itself (by
                accident or on purpose).
                But when I paste it into eclipse and run it
                eclipse's console, it doesn't work because answer seems to have a
                stray '\r' carriage return (CR) and therefore the comparison to 'no'
                fails.
                I get no 'compile' errors there.
                I get regular execution but it just doesn't change the
                condition to False at the very end.
                Therefore it loops forever. I used other values like zeros
                and ones to make sure I could print the values when the interpreter
                got down to that line. Everything checked. Just didn't change the
                condition on the main loop.

                Comment

                • richie

                  #9
                  Re: how to finish a while loop...

                  On Feb 20, 9:35 am, icarus <rsa...@gmail.c omwrote:
                  To the original poster.... what environment are you running this in?
                  >
                  Linux. Xubuntu if that matters.
                  >
                  When I put your program in notepad and run it from the windows command
                  prompt it works.
                  >
                  yeah yeah...same here.
                  After I got the tip that it actually worked, I went into the eclipse
                  directory where the program lives, ran it there from the shell, and it
                  worked. Meaning, I didn't modify anything on the file itself (by
                  accident or on purpose).
                  >
                  But when I paste it into eclipse and run it
                  eclipse's console, it doesn't work because answer seems to have a
                  stray '\r' carriage return (CR) and therefore the comparison to 'no'
                  fails.
                  >
                  I get no 'compile' errors there.
                  I get regular execution but it just doesn't change the
                  condition to False at the very end.
                  Therefore it loops forever. I used other values like zeros
                  and ones to make sure I could print the values when the interpreter
                  got down to that line. Everything checked. Just didn't change the
                  condition on the main loop.
                  I've changed this code a little.
                  condition = True
                  while ( condition ):
                  try:
                  integer_one = int ( raw_input( "Please enter an integer: " ) )
                  integer_two = int ( raw_input( "Please enter the second
                  integer: " ) )
                  division = integer_one / integer_two
                  except( ZeroDivisionErr or ):
                  print "\nDivision by zero detected"
                  except( ValueError ):
                  print "\nYou didn't enter an integer"
                  else:
                  print "The result is", division
                  answer = raw_input("Do you want to try again (yes or no)? ")
                  print answer
                  #answer="no"
                  if answer == "yes":
                  condition=True
                  elif answer == "no":
                  condition=False
                  print "Good bye, you don't want to continue"
                  print condition
                  And i got this result in eclipse3.2:
                  Please enter an integer: 8
                  Please enter the second integer: 4
                  The result is 2
                  Do you want to try again (yes or no)? no
                  no
                  Good bye, you don't want to continue
                  True
                  Please enter an integer:

                  it seems the input "no" in eclipse's console to answer won't equal the
                  "no" we compare.
                  And when I remove the comment and I get this result:
                  Please enter an integer: 8
                  Please enter the second integer: 4
                  The result is 2
                  Do you want to try again (yes or no)? no
                  no
                  Good bye, you don't want to continue
                  False

                  Comment

                  • rynt

                    #10
                    Re: how to finish a while loop...

                    Wouldn't hurt to send an email or message, along with the code, to
                    Fabio Zdronzy, the maintainer of PyDev explaining what happened.
                    He'll probably see this thread, but then again, maybe not.

                    Ruben


                    On Feb 19, 5:35 pm, icarus <rsa...@gmail.c omwrote:
                    To the original poster.... what environment are you running this in?
                    >
                                Linux.  Xubuntu if that matters.
                    >
                    When I put your program in notepad and run it from the windows command
                    prompt it works.
                    >
                                yeah yeah...same here.
                    After I got the tip that it actually worked, I went into the eclipse
                    directory where the program lives, ran it there from the shell, and it
                    worked.  Meaning, I didn't modify anything on the file itself (by
                    accident or on purpose).
                    >
                    But when I paste it into eclipse and run it
                    eclipse's console, it doesn't work because answer seems to have a
                    stray '\r' carriage return (CR) and therefore the comparison to 'no'
                    fails.
                    >
                              I get no 'compile' errors there.
                              I get regular execution but it just doesn't change the
                    condition to False at the very end.
                              Therefore it loops forever.  I used other values like zeros
                    and ones to make sure I could print the values when the interpreter
                    got down to that line.  Everything checked.  Just didn't change the
                    condition on the main loop.

                    Comment

                    • Preston  Landers

                      #11
                      Re: how to finish a while loop...

                      On Feb 19, 7:35 pm, icarus <rsa...@gmail.c omwrote:
                      But when I paste it into eclipse and run it
                      eclipse's console, it doesn't work because answer seems to have a
                      stray '\r' carriage return (CR) and therefore the comparison to 'no'
                      fails.
                      >
                      I get no 'compile' errors there.
                      I get regular execution but it just doesn't change the
                      condition to False at the very end.
                      Therefore it loops forever. I used other values like zeros
                      and ones to make sure I could print the values when the interpreter
                      got down to that line. Everything checked. Just didn't change the
                      condition on the main loop.
                      I'm pretty sure this is an eclipse console issue. Since you have it,
                      try stepping through the program in the debugger and inspect the
                      answer variable after raw_input returns. On my setup, at that point
                      answer is 'no\r', but raw_input() is supposed to strip line endings.
                      I'm guessing the console supplied 'no\r\n' and only the \n was
                      stripped. Could be that python does not recognize that eclipse's
                      console uses CRLF line endings.

                      As a workaround you can do raw_input().str ip().

                      You might check the eclipse issue tracker to see if this a known
                      issue.

                      Preston

                      Comment

                      • Chris

                        #12
                        Re: how to finish a while loop...

                        On Feb 20, 3:42 am, richie <richie...@gmai l.comwrote:
                        On Feb 20, 9:35 am, icarus <rsa...@gmail.c omwrote:
                        >
                        >
                        >
                        To the original poster.... what environment are you running this in?
                        >
                        Linux. Xubuntu if that matters.
                        >
                        When I put your program in notepad and run it from the windows command
                        prompt it works.
                        >
                        yeah yeah...same here.
                        After I got the tip that it actually worked, I went into the eclipse
                        directory where the program lives, ran it there from the shell, and it
                        worked. Meaning, I didn't modify anything on the file itself (by
                        accident or on purpose).
                        >
                        But when I paste it into eclipse and run it
                        eclipse's console, it doesn't work because answer seems to have a
                        stray '\r' carriage return (CR) and therefore the comparison to 'no'
                        fails.
                        >
                        I get no 'compile' errors there.
                        I get regular execution but it just doesn't change the
                        condition to False at the very end.
                        Therefore it loops forever. I used other values like zeros
                        and ones to make sure I could print the values when the interpreter
                        got down to that line. Everything checked. Just didn't change the
                        condition on the main loop.
                        >
                        I've changed this code a little.
                        condition = True
                        while ( condition ):
                        try:
                        integer_one = int ( raw_input( "Please enter an integer: " ) )
                        integer_two = int ( raw_input( "Please enter the second
                        integer: " ) )
                        division = integer_one / integer_two
                        except( ZeroDivisionErr or ):
                        print "\nDivision by zero detected"
                        except( ValueError ):
                        print "\nYou didn't enter an integer"
                        else:
                        print "The result is", division
                        answer = raw_input("Do you want to try again (yes or no)? ")
                        print answer
                        #answer="no"
                        if answer == "yes":
                        condition=True
                        elif answer == "no":
                        condition=False
                        print "Good bye, you don't want to continue"
                        print condition
                        And i got this result in eclipse3.2:
                        Please enter an integer: 8
                        Please enter the second integer: 4
                        The result is 2
                        Do you want to try again (yes or no)? no
                        no
                        Good bye, you don't want to continue
                        True
                        Please enter an integer:
                        >
                        it seems the input "no" in eclipse's console to answer won't equal the
                        "no" we compare.
                        And when I remove the comment and I get this result:
                        Please enter an integer: 8
                        Please enter the second integer: 4
                        The result is 2
                        Do you want to try again (yes or no)? no
                        no
                        Good bye, you don't want to continue
                        False
                        strip and lowercase your answer for better comparison checking.

                        Comment

                        • Martin Blume

                          #13
                          Re: how to finish a while loop...

                          "richie" schrieb
                          That code works. Maybe you fixed it while
                          you were mailing it =)
                          This is weird mate.
                          I'm using eclipse 3.2 with the pydev plugin.
                          There it loops forever - from the eclipse console.
                          Two hours of trying, changing the code...finally gave up.

                          Then I got your reply. Opened up a regular console
                          and executed it from there.
                          And voila....it works! Well, after this I'm going
                          back to the old trusty shell.

                          Thanks again mate.
                          >
                          I try it too in my eclipse3.2. I got the same result.
                          It seems very strange.
                          >
                          Print out "answer" and see if there is a difference ...

                          my $0.02
                          Martin


                          Comment

                          • Jeff Schwab

                            #14
                            Re: how to finish a while loop...

                            icarus wrote:
                            Opened up a regular console and executed it
                            from there.
                            And voila....it works! Well, after this I'm going back to the old
                            trusty shell.
                            +1 QOTW

                            Comment

                            Working...