Newb ??

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

    #1

    Newb ??

    Hi all, I am new to the group. Trying to learn Python programming on my
    own. I am working through Michael Dawson's Book Python Programming for the
    absolute beginner.

    I am tring to write a program that is a number guessing game. I want to be
    able to give the user 5 tries to guess the number before the program ends.

    I get the result that I want when the user misses after the 5th try. But it
    does not go down to my closing statement to end. Rather is askes for
    another guess.

    I appreciate any help you can give. If this is not the correct group for
    these types of questions, I apologize and hope that you can direct me to
    another NG.

    Thanks,
    Chad

    import random

    print "\tWelcome to 'Guess my Number'!"
    print "\nI'm Thinking of a Number between 1 and 100."
    print "\nTry to Guess it in as few attempts as possible.\n"

    #Set the initial values
    the_number = random.randrang e(100) + 1
    guess = int(raw_input(" Take a guess:"))
    tries = 1

    #Guessing Loop

    while (guess != the_number):
    if (guess >the_number):
    print "Lower..."
    else:
    print "Higher..."
    guess = int(raw_input(" Take another Guess:"))
    tries +=1
    if tries == 5:
    print "Sorry you lose."
    print "The Correct Number was ", the_number


    print "You guess correctly!!"
    print "The number was:", the_number
    print "You got it correct in", tries, "tries"

    raw_input("Pres s Enter to exit the program")


  • mensanator@aol.com

    #2
    Re: Newb ??


    Chad Everett wrote:[color=blue]
    > Hi all, I am new to the group. Trying to learn Python programming on my
    > own. I am working through Michael Dawson's Book Python Programming for the
    > absolute beginner.
    >
    > I am tring to write a program that is a number guessing game. I want to be
    > able to give the user 5 tries to guess the number before the program ends.
    >
    > I get the result that I want when the user misses after the 5th try. But it
    > does not go down to my closing statement to end. Rather is askes for
    > another guess.[/color]

    Because even after 5 tries, guess != the_number. You won't get past
    the while loop until it does. When there have been 5 misses, try
    setting
    guess=the_numbe r.
    [color=blue]
    >
    > I appreciate any help you can give. If this is not the correct group for
    > these types of questions, I apologize and hope that you can direct me to
    > another NG.
    >
    > Thanks,
    > Chad
    >
    > import random
    >
    > print "\tWelcome to 'Guess my Number'!"
    > print "\nI'm Thinking of a Number between 1 and 100."
    > print "\nTry to Guess it in as few attempts as possible.\n"
    >
    > #Set the initial values
    > the_number = random.randrang e(100) + 1
    > guess = int(raw_input(" Take a guess:"))
    > tries = 1
    >
    > #Guessing Loop
    >
    > while (guess != the_number):
    > if (guess >the_number):
    > print "Lower..."
    > else:
    > print "Higher..."
    > guess = int(raw_input(" Take another Guess:"))
    > tries +=1
    > if tries == 5:
    > print "Sorry you lose."
    > print "The Correct Number was ", the_number
    >
    >
    > print "You guess correctly!!"
    > print "The number was:", the_number
    > print "You got it correct in", tries, "tries"
    >
    > raw_input("Pres s Enter to exit the program")[/color]

    Comment

    • jmdeschamps@gmail.com

      #3
      Re: Newb ??


      mensanator@aol. com wrote:[color=blue]
      > Chad Everett wrote:[color=green]
      > > Hi all, I am new to the group. Trying to learn Python programming on my
      > > own. I am working through Michael Dawson's Book Python Programming for the
      > > absolute beginner.
      > >
      > > I am tring to write a program that is a number guessing game. I want to be
      > > able to give the user 5 tries to guess the number before the program ends.
      > >
      > > I get the result that I want when the user misses after the 5th try. But it
      > > does not go down to my closing statement to end. Rather is askes for
      > > another guess.[/color]
      >
      > Because even after 5 tries, guess != the_number. You won't get past
      > the while loop until it does. When there have been 5 misses, try
      > setting
      > guess=the_numbe r.
      >[color=green]
      > >
      > > I appreciate any help you can give. If this is not the correct group for
      > > these types of questions, I apologize and hope that you can direct me to
      > > another NG.
      > >
      > > Thanks,
      > > Chad
      > >
      > > import random
      > >
      > > print "\tWelcome to 'Guess my Number'!"
      > > print "\nI'm Thinking of a Number between 1 and 100."
      > > print "\nTry to Guess it in as few attempts as possible.\n"
      > >
      > > #Set the initial values
      > > the_number = random.randrang e(100) + 1
      > > guess = int(raw_input(" Take a guess:"))
      > > tries = 1
      > >
      > > #Guessing Loop
      > >
      > > while (guess != the_number):
      > > if (guess >the_number):
      > > print "Lower..."
      > > else:
      > > print "Higher..."
      > > guess = int(raw_input(" Take another Guess:"))
      > > tries +=1
      > > if tries == 5:
      > > print "Sorry you lose."
      > > print "The Correct Number was ", the_number
      > >
      > >
      > > print "You guess correctly!!"
      > > print "The number was:", the_number
      > > print "You got it correct in", tries, "tries"
      > >
      > > raw_input("Pres s Enter to exit the program")[/color][/color]

      here is your corrected version:
      ##FROM here it's good
      import random

      print "\tWelcome to 'Guess my Number'!"
      print "\nI'm Thinking of a Number between 1 and 100."
      print "\nTry to Guess it in as few attempts as possible.\n"

      #Set the initial values
      ## Here you always get 100, randrange is from start to end+1
      the_number = random.randrang e(0,101)
      ##This is OK
      guess = int(raw_input(" Take a guess:"))
      tries = 1

      #Guessing Loop

      while (guess != the_number):
      if (guess >the_number):
      print "Lower..."
      else:
      print "Higher..."
      guess = int(raw_input(" Take another Guess:"))
      tries +=1
      if tries == 5:
      ## Missing break to explicitly leave loop after 5
      break
      ## Then test to see if last guess equals the_number
      if guess != the_number:
      print "Sorry you lose."
      print "The Correct Number was ", the_number
      else:
      print "You guess correctly!!"
      print "The number was:", the_number
      print "You got it correct in", tries, "tries"

      raw_input("Pres s Enter to exit the program")

      Comment

      • Steve Holden

        #4
        Re: Newb ??

        Chad Everett wrote:[color=blue]
        > Hi all, I am new to the group. Trying to learn Python programming on my
        > own. I am working through Michael Dawson's Book Python Programming for the
        > absolute beginner.
        >
        > I am tring to write a program that is a number guessing game. I want to be
        > able to give the user 5 tries to guess the number before the program ends.
        >
        > I get the result that I want when the user misses after the 5th try. But it
        > does not go down to my closing statement to end. Rather is askes for
        > another guess.
        >
        > I appreciate any help you can give. If this is not the correct group for
        > these types of questions, I apologize and hope that you can direct me to
        > another NG.
        >
        > Thanks,
        > Chad
        >
        > import random
        >
        > print "\tWelcome to 'Guess my Number'!"
        > print "\nI'm Thinking of a Number between 1 and 100."
        > print "\nTry to Guess it in as few attempts as possible.\n"
        >
        > #Set the initial values
        > the_number = random.randrang e(100) + 1
        > guess = int(raw_input(" Take a guess:"))
        > tries = 1
        >
        > #Guessing Loop
        >
        > while (guess != the_number):
        > if (guess >the_number):
        > print "Lower..."
        > else:
        > print "Higher..."
        > guess = int(raw_input(" Take another Guess:"))
        > tries +=1
        > if tries == 5:
        > print "Sorry you lose."
        > print "The Correct Number was ", the_number
        >
        >
        > print "You guess correctly!!"
        > print "The number was:", the_number
        > print "You got it correct in", tries, "tries"
        >
        > raw_input("Pres s Enter to exit the program")
        >
        >[/color]
        I don't personally think the two possible solutions you've been given
        are very pythonic. All you really need to do to get the loop termination
        conditions right (he says confidently, and with absolutely no testing at
        all) is change the while line to

        while guess != the_number and tries <=5:

        but there are other things wrong with the program. In "pseudo-code"
        (stuff that helps you think about the problem without being able to feed
        it into an interpreter) your solution should be

        while not guessed and guesses < 5
        guess number
        if guessed
        congratulate user
        else
        crow and rub user's face in it

        At present your code will always tell the user she guessed correctly,
        even after it's told her she lost. See if this (tested) rewrite makes
        sense to you:


        import random

        print "\tWelcome to 'Guess my Number'!"
        print "\nI'm Thinking of a Number between 1 and 100."
        print "\nTry to Guess it in as few attempts as possible.\n"

        #Set the initial values
        the_number = random.randrang e(100) + 1
        guess = int(raw_input(" Take a guess:"))
        tries = 1

        #Guessing Loop

        while guess != the_number and tries < 5:
        if (guess >the_number):
        print "Lower..."
        else:
        print "Higher..."
        guess = int(raw_input(" Take another Guess:"))
        tries +=1

        if guess == the_number:
        print "You guess correctly!!"
        print "The number was:", the_number
        print "You got it correct in", tries, "tries"
        else:
        print "Sorry you lose."
        print "The Correct Number was ", the_number

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC www.holdenweb.com
        PyCon TX 2006 www.python.org/pycon/

        Comment

        • bonono@gmail.com

          #5
          Re: Newb ??

          I would rather to do the loop as :

          max_try = 5
          for x in xrange(max_try) :
          g = int(raw_input(["Take a guess","Take another guess"][x != 0]))
          if g == number:
          print "bingo, hit it in %i tries" % x + 1
          break
          elif g < number: print "try higher"
          else: print "try lower"
          else: print "sorry, you lose, the number is %i" % number

          Chad Everett wrote:
          [color=blue]
          > import random
          >
          > print "\tWelcome to 'Guess my Number'!"
          > print "\nI'm Thinking of a Number between 1 and 100."
          > print "\nTry to Guess it in as few attempts as possible.\n"
          >
          > #Set the initial values
          > the_number = random.randrang e(100) + 1
          > guess = int(raw_input(" Take a guess:"))
          > tries = 1
          >
          > #Guessing Loop
          >
          > while (guess != the_number):
          > if (guess >the_number):
          > print "Lower..."
          > else:
          > print "Higher..."
          > guess = int(raw_input(" Take another Guess:"))
          > tries +=1
          > if tries == 5:
          > print "Sorry you lose."
          > print "The Correct Number was ", the_number
          >
          >
          > print "You guess correctly!!"
          > print "The number was:", the_number
          > print "You got it correct in", tries, "tries"
          >
          > raw_input("Pres s Enter to exit the program")[/color]

          Comment

          • jmdeschamps@gmail.com

            #6
            Re: Newb ??


            ## Here you always get 100...

            This is false , sorry for the wrong comment on this part, it should
            rather be:
            ## randrange is from start to end-1
            the_number = random.randrang e(1,101)
            JM
            (But the rest of my comment seems OK)

            Comment

            • Norman Silverstone

              #7
              Re: Newb ??

              :[color=blue][color=green][color=darkred]
              >> > Hi all, I am new to the group. Trying to learn Python programming on my
              >> > own. I am working through Michael Dawson's Book Python Programming for the
              >> > absolute beginner.
              >> >
              >> > I am tring to write a program that is a number guessing game. I want to be
              >> > able to give the user 5 tries to guess the number before the program ends.[/color][/color][/color]

              < snip >

              I am also working my way slowly through this book which I find quite
              challenging. What I am thinking about at the moment is how to program for
              the computer to guess the number which I select. I know it can be done but
              I suspect it requires an approach which I have not yet learnt. I would
              welcome suggestions on the best way to approach this problem.

              Norman

              Comment

              • Chad Everett

                #8
                Re: Newb ??

                Hey guys,

                Thanks for your help. I am glad to know there is a community out there
                willing to put the effort out to help us newbies. I will work on it. Some
                of the suggestions are things that I have not covered yet but I am sure I
                will figure it out.

                Thanks again.

                I am sure I will be back with more questions before too long.

                Chad


                "Chad Everett" <everettcc@hotm ail.com> wrote in message
                news:mzdcf.470$ rN2.320@bignews 2.bellsouth.net ...[color=blue]
                > Hi all, I am new to the group. Trying to learn Python programming on my
                > own. I am working through Michael Dawson's Book Python Programming for
                > the absolute beginner.
                >
                > I am tring to write a program that is a number guessing game. I want to
                > be able to give the user 5 tries to guess the number before the program
                > ends.
                >
                > I get the result that I want when the user misses after the 5th try. But
                > it does not go down to my closing statement to end. Rather is askes for
                > another guess.
                >
                > I appreciate any help you can give. If this is not the correct group for
                > these types of questions, I apologize and hope that you can direct me to
                > another NG.
                >
                > Thanks,
                > Chad
                >
                > import random
                >
                > print "\tWelcome to 'Guess my Number'!"
                > print "\nI'm Thinking of a Number between 1 and 100."
                > print "\nTry to Guess it in as few attempts as possible.\n"
                >
                > #Set the initial values
                > the_number = random.randrang e(100) + 1
                > guess = int(raw_input(" Take a guess:"))
                > tries = 1
                >
                > #Guessing Loop
                >
                > while (guess != the_number):
                > if (guess >the_number):
                > print "Lower..."
                > else:
                > print "Higher..."
                > guess = int(raw_input(" Take another Guess:"))
                > tries +=1
                > if tries == 5:
                > print "Sorry you lose."
                > print "The Correct Number was ", the_number
                >
                >
                > print "You guess correctly!!"
                > print "The number was:", the_number
                > print "You got it correct in", tries, "tries"
                >
                > raw_input("Pres s Enter to exit the program")
                >[/color]


                Comment

                • Alex Martelli

                  #9
                  Re: Newb ??

                  Norman Silverstone <norman@littlet ank.org> wrote:
                  ...[color=blue]
                  > challenging. What I am thinking about at the moment is how to program for
                  > the computer to guess the number which I select. I know it can be done but
                  > I suspect it requires an approach which I have not yet learnt. I would
                  > welcome suggestions on the best way to approach this problem.[/color]

                  I assume the way the computer is going to guess is by trying some
                  number, and you respond either that it's guessed right, or to go lower,
                  or to go higher.

                  In that case, think of "bisection" . Originally, all the computer knows
                  is that the number is in some range, say 0 to 100. It can then guess
                  the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
                  lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
                  each case the range was just halved (actually, a bit more than halved).

                  It does not take many halvings to squeeze even a pretty large original
                  range down to a range which only includes one possible number... that's
                  because "2 to the power of N" grows VERY fast with N, as the old story
                  about the inventor of chess shows (the king told him to ask for a
                  reward, and all he wanted was some rice -- one grain on the first cell
                  of the chessboard, two on the next, then four, eight... all the way
                  through the 64 squares of the board; the kind thought the inventor was
                  being very moderate in his request, and granted it... to soon find out
                  that not enough rice existed in the world to satisfy the request...;-).

                  Well, repeated halving is just like repeated doubling "backwards" , so it
                  squeezes vast ranges of possibilities down to tiny ones just as fast as
                  repeated doubling produces oceans of rice from a small chessboard;-).


                  Alex

                  Comment

                  • Norman Silverstone

                    #10
                    Re: Newb ??

                    < snip>
                    [color=blue]
                    > I assume the way the computer is going to guess is by trying some
                    > number, and you respond either that it's guessed right, or to go lower,
                    > or to go higher.[/color]

                    Yes, that is correct.[color=blue]
                    >
                    > In that case, think of "bisection" . Originally, all the computer knows
                    > is that the number is in some range, say 0 to 100. It can then guess
                    > the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
                    > lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
                    > each case the range was just halved (actually, a bit more than halved).[/color]

                    Thank you, I thought that might be the case. So, I will have to settle
                    down and try to write some pseudo-code first. I hope to be back.

                    Norman

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: Newb ??

                      On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:
                      [color=blue][color=green]
                      >> In that case, think of "bisection" . Originally, all the computer knows
                      >> is that the number is in some range, say 0 to 100. It can then guess
                      >> the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
                      >> lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
                      >> each case the range was just halved (actually, a bit more than halved).[/color]
                      >
                      > Thank you, I thought that might be the case. So, I will have to settle
                      > down and try to write some pseudo-code first. I hope to be back.[/color]

                      Heh, you will find that Python is practically executable pseudo-code!

                      Untested:


                      def guess_number():
                      # please don't cheat the poor computer...
                      print "Guess a number."
                      lo = 0
                      hi = 100
                      while True:
                      guess = (lo+hi)//2
                      ans = raw_input("Is it %d? y/n " % guess)
                      if ans in ('y', 'yes'):
                      break
                      ans = raw_input("Too high? y/n ")
                      if ans in ("y", "yes"):
                      hi = guess-1
                      else:
                      lo = guess+1

                      This should run, and it will *almost* do what you want.


                      --
                      Steven.

                      Comment

                      • Norman Silverstone

                        #12
                        Re: Newb ??

                        On Fri, 11 Nov 2005 01:39:40 +1100, Steven D'Aprano wrote:
                        [color=blue]
                        > On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:
                        >[color=green][color=darkred]
                        >>> In that case, think of "bisection" . Originally, all the computer knows
                        >>> is that the number is in some range, say 0 to 100. It can then guess
                        >>> the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
                        >>> lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
                        >>> each case the range was just halved (actually, a bit more than halved).[/color]
                        >>
                        >> Thank you, I thought that might be the case. So, I will have to settle
                        >> down and try to write some pseudo-code first. I hope to be back.[/color]
                        >
                        > Heh, you will find that Python is practically executable pseudo-code!
                        >
                        > Untested:
                        >
                        >
                        > def guess_number():
                        > # please don't cheat the poor computer...
                        > print "Guess a number."
                        > lo = 0
                        > hi = 100
                        > while True:
                        > guess = (lo+hi)//2
                        > ans = raw_input("Is it %d? y/n " % guess)
                        > if ans in ('y', 'yes'):
                        > break
                        > ans = raw_input("Too high? y/n ")
                        > if ans in ("y", "yes"):
                        > hi = guess-1
                        > else:
                        > lo = guess+1
                        >
                        > This should run, and it will *almost* do what you want.[/color]

                        Thanks for that but I think it is too simplistic. It appears OK for the
                        first guess, which is 50 but, what about the next guess. If the guess is
                        too high then the next guess has to be 50/2. However, if it is too low
                        then the next guess must be first guess + (100-second guess)/2. In general
                        terms, if guess is too high then next guess must (guess - lowest
                        possible)/2 and if too low then it is guess + (highest possible -
                        guess)/2.

                        Comments please.

                        Norman

                        Comment

                        • Steve Holden

                          #13
                          Re: Newb ??

                          Norman Silverstone wrote:[color=blue]
                          > On Fri, 11 Nov 2005 01:39:40 +1100, Steven D'Aprano wrote:
                          >
                          >[color=green]
                          >>On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:
                          >>
                          >>[color=darkred]
                          >>>>In that case, think of "bisection" . Originally, all the computer knows
                          >>>>is that the number is in some range, say 0 to 100. It can then guess
                          >>>>the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
                          >>>>lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
                          >>>>each case the range was just halved (actually, a bit more than halved).
                          >>>
                          >>>Thank you, I thought that might be the case. So, I will have to settle
                          >>>down and try to write some pseudo-code first. I hope to be back.[/color]
                          >>
                          >>Heh, you will find that Python is practically executable pseudo-code!
                          >>
                          >>Untested:
                          >>
                          >>
                          >>def guess_number():
                          >> # please don't cheat the poor computer...
                          >> print "Guess a number."
                          >> lo = 0
                          >> hi = 100
                          >> while True:
                          >> guess = (lo+hi)//2
                          >> ans = raw_input("Is it %d? y/n " % guess)
                          >> if ans in ('y', 'yes'):
                          >> break
                          >> ans = raw_input("Too high? y/n ")
                          >> if ans in ("y", "yes"):
                          >> hi = guess-1
                          >> else:
                          >> lo = guess+1
                          >>
                          >>This should run, and it will *almost* do what you want.[/color]
                          >
                          >
                          > Thanks for that but I think it is too simplistic. It appears OK for the
                          > first guess, which is 50 but, what about the next guess. If the guess is
                          > too high then the next guess has to be 50/2. However, if it is too low
                          > then the next guess must be first guess + (100-second guess)/2. In general
                          > terms, if guess is too high then next guess must (guess - lowest
                          > possible)/2 and if too low then it is guess + (highest possible -
                          > guess)/2.
                          >
                          > Comments please.
                          >
                          > Norman
                          >[/color]
                          Effectively you want to start with a minposs and maxposs, which are set
                          to 0 and 100 respectively. Your guess should bisect the range (as nearly
                          as it can given that you are dealing with integers, and you have to be
                          careful to get the awkward boundary conditions right). So your first
                          guess will be 50. If your guess is too low then replace minposs with
                          your guess (in this case 50); if too high, replace maxposs with your
                          guess; loop to generate the next guess, and so on. In practice since
                          log2(100) > 5 your five guesses won't always be enough (though seven
                          should be).

                          Hope this helps.

                          regards
                          Steve
                          --
                          Steve Holden +44 150 684 7255 +1 800 494 3119
                          Holden Web LLC www.holdenweb.com
                          PyCon TX 2006 www.python.org/pycon/

                          Comment

                          • Fredrik Lundh

                            #14
                            Re: Newb ??

                            Norman Silverstone wrote:[color=blue][color=green]
                            > > Heh, you will find that Python is practically executable pseudo-code!
                            > >
                            > > Untested:
                            > >
                            > >
                            > > def guess_number():
                            > > # please don't cheat the poor computer...
                            > > print "Guess a number."
                            > > lo = 0
                            > > hi = 100
                            > > while True:
                            > > guess = (lo+hi)//2
                            > > ans = raw_input("Is it %d? y/n " % guess)
                            > > if ans in ('y', 'yes'):
                            > > break
                            > > ans = raw_input("Too high? y/n ")
                            > > if ans in ("y", "yes"):
                            > > hi = guess-1
                            > > else:
                            > > lo = guess+1
                            > >
                            > > This should run, and it will *almost* do what you want.[/color]
                            >
                            > Thanks for that but I think it is too simplistic. It appears OK for the
                            > first guess, which is 50 but, what about the next guess.[/color]

                            did you test the script?


                            If the guess is[color=blue]
                            > too high then the next guess has to be 50/2. However, if it is too low
                            > then the next guess must be first guess + (100-second guess)/2. In general
                            > terms, if guess is too high then next guess must (guess - lowest
                            > possible)/2 and if too low then it is guess + (highest possible -
                            > guess)/2.
                            >
                            > Comments please.
                            >
                            > Norman
                            >
                            > --
                            > http://mail.python.org/mailman/listinfo/python-list
                            >[/color]



                            Comment

                            • Fredrik Lundh

                              #15
                              Re: Newb ??

                              Norman Silverstone wrote:
                              [color=blue][color=green]
                              > > Heh, you will find that Python is practically executable pseudo-code!
                              > >
                              > > Untested:
                              > >
                              > >
                              > > def guess_number():
                              > > # please don't cheat the poor computer...
                              > > print "Guess a number."
                              > > lo = 0
                              > > hi = 100
                              > > while True:
                              > > guess = (lo+hi)//2
                              > > ans = raw_input("Is it %d? y/n " % guess)
                              > > if ans in ('y', 'yes'):
                              > > break
                              > > ans = raw_input("Too high? y/n ")
                              > > if ans in ("y", "yes"):
                              > > hi = guess-1
                              > > else:
                              > > lo = guess+1
                              > >
                              > > This should run, and it will *almost* do what you want.[/color]
                              >
                              > Thanks for that but I think it is too simplistic. It appears OK for the
                              > first guess, which is 50 but, what about the next guess.[/color]

                              did you test the script? here's a simulator:

                              import re

                              number = 0
                              guess = None
                              count = 0

                              def raw_input(promp t):
                              global count, guess
                              reply = "n"
                              m = re.search("\d+" , prompt)
                              if m:
                              # guess a number
                              count = count + 1
                              guess = int(m.group())
                              if guess == number:
                              reply = "y"
                              else:
                              # check if too high
                              if guess > number:
                              reply = "y"
                              print prompt, reply
                              return reply

                              def guess_number():
                              # please don't cheat the poor computer...
                              print "Guess a number."
                              lo = 0
                              hi = 100
                              while True:
                              guess = (lo+hi)//2
                              ans = raw_input("Is it %d? y/n " % guess)
                              if ans in ('y', 'yes'):
                              break
                              ans = raw_input("Too high? y/n ")
                              if ans in ("y", "yes"):
                              hi = guess-1
                              else:
                              lo = guess+1

                              for number in range(0, 100+1):
                              print "NUMBER IS", number
                              count = 0
                              guess_number()
                              print "SUCCESS AFTER", count, "GUESSES"

                              # end
                              [color=blue]
                              > Comments please.[/color]

                              if this had been a java "let's pretend you're the java runtime" certification
                              question, you would have failed.

                              </F>



                              Comment

                              Working...