Guess My Number Game

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

    Guess My Number Game

    Hey, I'm new to python (and programming in general) so I'll prolly be around
    here a lot...

    Anyways, I've found out how to make a "guess my number game" where the
    player guesses a number between 1 and 100, but I want to switch things
    around. I want to be able to put in my own number and have the computer
    guess it. I already know how to make it guess (by using randrange) but I'm
    having a hard time making it smarter. (Like guessing higher or lower based
    on what it's told it needs to do.) Here's the code I have right now:

    _______________ _______________ _______________ _____________


    import random

    guess = 0
    tries = 0
    number = input("Pick a number between 1 and 100 for the computer to guess:
    ")

    while number > 100 or number < 1:
    number = input("Pick a number between 1 and 100 for the computer to guess:
    ")

    while guess != number:
    guess = random.randrang e(101)
    print "The computer guessed", guess
    tries += 1
    past = guess
    while guess < number:
    guess = random.randrang e(guess, 101)
    print "The computer guessed", guess
    tries += 1
    while guess > number:
    guess = random.randrang e(0, guess)
    print "The computer guessed", guess
    tries += 1

    print "The computer guessed your number after", tries, "tries."

    raw_input("Pres s enter to exit.")

    _______________ _______________ _______________ _______________


    As you can see, I've already made it a little smarter but I think I could
    still mae it better. Any ideas?

    Also, does anyone know a really popular python forum?


  • F. GEIGER

    #2
    Re: Guess My Number Game

    *Many* years ago I owned a TI 58C pocket calculator (anyone remember
    these?). It had the game you speak about built into its rom. It used
    bisection: You had to choose a number and than tell the computer if the
    number it guessed was less than or greater than or equal the one you had
    chosen.

    Cheers
    Franz GEIGER


    "EAS" <eriksp@attbi.n ospam.com> schrieb im Newsbeitrag
    news:u9ppc.5447 $gr.390828@attb i_s52...[color=blue]
    > Hey, I'm new to python (and programming in general) so I'll prolly be[/color]
    around[color=blue]
    > here a lot...
    >
    > Anyways, I've found out how to make a "guess my number game" where the
    > player guesses a number between 1 and 100, but I want to switch things
    > around. I want to be able to put in my own number and have the computer
    > guess it. I already know how to make it guess (by using randrange) but I'm
    > having a hard time making it smarter. (Like guessing higher or lower based
    > on what it's told it needs to do.) Here's the code I have right now:
    >
    > _______________ _______________ _______________ _____________
    >
    >
    > import random
    >
    > guess = 0
    > tries = 0
    > number = input("Pick a number between 1 and 100 for the computer to guess:
    > ")
    >
    > while number > 100 or number < 1:
    > number = input("Pick a number between 1 and 100 for the computer to guess:
    > ")
    >
    > while guess != number:
    > guess = random.randrang e(101)
    > print "The computer guessed", guess
    > tries += 1
    > past = guess
    > while guess < number:
    > guess = random.randrang e(guess, 101)
    > print "The computer guessed", guess
    > tries += 1
    > while guess > number:
    > guess = random.randrang e(0, guess)
    > print "The computer guessed", guess
    > tries += 1
    >
    > print "The computer guessed your number after", tries, "tries."
    >
    > raw_input("Pres s enter to exit.")
    >
    > _______________ _______________ _______________ _______________
    >
    >
    > As you can see, I've already made it a little smarter but I think I could
    > still mae it better. Any ideas?
    >
    > Also, does anyone know a really popular python forum?
    >
    >[/color]


    Comment

    • moma

      #3
      Re: Guess My Number Game

      EAS wrote:[color=blue]
      > Hey, I'm new to python (and programming in general) so I'll prolly be around
      > here a lot...
      >
      > Anyways, I've found out how to make a "guess my number game" where the
      > player guesses a number between 1 and 100, but I want to switch things
      > around. I want to be able to put in my own number and have the computer
      > guess it. I already know how to make it guess (by using randrange) but I'm
      > having a hard time making it smarter. (Like guessing higher or lower based
      > on what it's told it needs to do.) Here's the code I have right now:
      >
      > _______________ _______________ _______________ _____________
      >
      >
      > import random
      >
      > guess = 0
      > tries = 0
      > number = input("Pick a number between 1 and 100 for the computer to guess:
      > ")
      >
      > while number > 100 or number < 1:
      > number = input("Pick a number between 1 and 100 for the computer to guess:
      > ")
      >
      > while guess != number:
      > guess = random.randrang e(101)
      > print "The computer guessed", guess
      > tries += 1
      > past = guess
      > while guess < number:
      > guess = random.randrang e(guess, 101)
      > print "The computer guessed", guess
      > tries += 1
      > while guess > number:
      > guess = random.randrang e(0, guess)
      > print "The computer guessed", guess
      > tries += 1
      >
      > print "The computer guessed your number after", tries, "tries."
      >
      > raw_input("Pres s enter to exit.")
      >
      > _______________ _______________ _______________ _______________
      >
      >
      > As you can see, I've already made it a little smarter but I think I could
      > still mae it better. Any ideas?
      >
      > Also, does anyone know a really popular python forum?
      >[/color]

      Hello all,

      I^am also traing Python. Veeery newbie in Python.
      Here is a variant of your guessing game.


      # Do not forget those TABS or SPACES !
      # Thee seems to be important in Python.

      # --- Beg -----------------------------
      #!/usr/bin/python
      # Name guess1.py

      maxnum = 100
      minnum = 1

      number = 0
      while number > maxnum+1 or number < minnum:
      number = input("Pick a number between 1 and 100 for the computer to
      guess:")

      # guess = int(raw_input(' Enter an integer : '))

      done = False
      guess = tries = 0

      guess = (maxnum - minnum + 1) / 2
      while not done:
      # guess = random.randrang e(1, 101)
      print "my guess is ", guess, " Number = ", number

      if guess > number:
      print "It`s less than ", guess
      maxnum = guess
      guess = maxnum - (maxnum - minnum + 1) / 2
      elif guess < number:
      print "It`s more than ", guess
      minnum = guess
      guess = minnum + (maxnum - minnum + 1) / 2
      else:
      print "Knowing you knowing me, I guessed right !"
      done = True

      tries = tries + 1

      print "The computer guessed your number after", tries, "tries."

      raw_input("Pres s enter to exit.")

      # --- End -----------------------------


      // moma
      http://www.futuredesktop.org :: newbie links at the top



      Comment

      • moma

        #4
        Re: Guess My Number Game

        EAS wrote:[color=blue]
        > Hey, I'm new to python (and programming in general) so I'll prolly be around
        > here a lot...
        >
        > Anyways, I've found out how to make a "guess my number game" where the
        > player guesses a number between 1 and 100, but I want to switch things
        > around. I want to be able to put in my own number and have the computer
        > guess it. I already know how to make it guess (by using randrange) but I'm
        > having a hard time making it smarter. (Like guessing higher or lower based
        > on what it's told it needs to do.) Here's the code I have right now:
        >
        > _______________ _______________ _______________ _____________
        >
        >
        > import random
        >
        > guess = 0
        > tries = 0
        > number = input("Pick a number between 1 and 100 for the computer to guess:
        > ")
        >
        > while number > 100 or number < 1:
        > number = input("Pick a number between 1 and 100 for the computer to guess:
        > ")
        >
        > while guess != number:
        > guess = random.randrang e(101)
        > print "The computer guessed", guess
        > tries += 1
        > past = guess
        > while guess < number:
        > guess = random.randrang e(guess, 101)
        > print "The computer guessed", guess
        > tries += 1
        > while guess > number:
        > guess = random.randrang e(0, guess)
        > print "The computer guessed", guess
        > tries += 1
        >
        > print "The computer guessed your number after", tries, "tries."
        >
        > raw_input("Pres s enter to exit.")
        >
        > _______________ _______________ _______________ _______________
        >
        >
        > As you can see, I've already made it a little smarter but I think I could
        > still mae it better. Any ideas?
        >
        > Also, does anyone know a really popular python forum?
        >
        >[/color]

        Hello all,

        I^am also traing Python. Veeery newbie in Python.
        Here is a variant of your guessing game.


        # Do not forget those TABS or SPACES !
        # Thee seems to be important.

        # --- Beg -----------------------------
        #!/usr/bin/python
        # Name guess1.py

        maxnum = 100
        minnum = 1

        number = 0
        while number > maxnum+1 or number < minnum:
        number = input("Pick a number between 1 and 100 for the computer to
        guess:")

        # guess = int(raw_input(' Enter an integer : '))

        done = False
        guess = tries = 0
        guess = (maxnum - minnum + 1) / 2

        while not done:
        print "My guess is ", guess

        if guess > number:
        print "It`s less than ", guess
        maxnum = guess
        guess = maxnum - (maxnum - minnum + 1) / 2
        elif guess < number:
        print "It`s more than ", guess
        minnum = guess
        guess = minnum + (maxnum - minnum + 1) / 2
        else:
        print "Knowing you knowing me, I guessed right !"
        done = True

        tries = tries + 1

        print "The computer guessed your number after", tries, "tries."

        raw_input("Pres s enter to exit.")

        # --- End -----------------------------


        // moma
        http://www.futuredesktop.org :: newbie links at the top



        Comment

        • Heiko Wundram

          #5
          Re: Guess My Number Game

          Am Samstag, 15. Mai 2004 15:44 schrieb EAS:[color=blue]
          > Anyways, I've found out how to make a "guess my number game" where the
          > player guesses a number between 1 and 100, but I want to switch things
          > around. I want to be able to put in my own number and have the computer
          > guess it. I already know how to make it guess (by using randrange) but I'm
          > having a hard time making it smarter. (Like guessing higher or lower based
          > on what it's told it needs to do.) Here's the code I have right now:[/color]

          On average, the shortest possible runtime (and also a deterministic runtime of
          O(log2(high-low))) will be achieved if you use interval walking.

          Thus:

          number = 64
          low = 1
          high = 100
          while low <> high:
          med = (low+high)//2
          tries += 1
          if med == number:
          print "I guessed your number:", med
          elif med < number:
          if med == low:
          print "I guessed your number:", med+1
          low = med+1
          else:
          low = med
          else:
          high = med
          print "I needed", tries, "tries."

          HTH!

          Heiko.

          Comment

          • Andrea Griffini

            #6
            Re: Guess My Number Game

            On Sat, 15 May 2004 15:58:22 +0200, Heiko Wundram <heikowu@ceosg. de>
            wrote:

            Not really python related, but...
            [color=blue]
            >On average, the shortest possible runtime (and also a deterministic runtime of
            >O(log2(high-low))) will be achieved if you use interval walking.
            >
            >Thus:
            >
            >number = 64
            >low = 1
            >high = 100
            >while low <> high:
            > med = (low+high)//2
            > tries += 1
            > if med == number:
            > print "I guessed your number:", med
            > elif med < number:
            > if med == low:
            > print "I guessed your number:", med+1
            > low = med+1
            > else:
            > low = med
            > else:
            > high = med
            >print "I needed", tries, "tries."[/color]

            This bisection algorithm is bad. On average it will require
            about 0.5 more steps than necessary. A better one is...

            def dico2(low,high, guess):
            while low < high:
            t = (low + high)//2
            g = guess(t)
            if g == 0:
            return t
            elif g == -1:
            low = t+1
            else:
            high = t-1
            return low

            supposing that guess(x) returns -1, 0 or 1 depending
            if the number is too low, correct or too high.

            If you find yourself handling special cases in a
            bisection algorithm (e.g. your test "if med==low")
            then you can be quite sure there's a better way.

            HTH
            Andrea

            Comment

            • Andrew Dalke

              #7
              Re: Guess My Number Game

              Andrea Griffini[color=blue]
              > Not really python related, but...
              >[color=green]
              > >On average, the shortest possible runtime (and also a deterministic[/color][/color]
              runtime of[color=blue][color=green]
              > >O(log2(high-low))) will be achieved if you use interval walking.[/color][/color]
              ...[color=blue]
              > supposing that guess(x) returns -1, 0 or 1 depending
              > if the number is too low, correct or too high.[/color]

              And not really an answer to the OP; here's a solution which uses
              the bisect module, letting it do the heavy work. It uses the same sort
              of guess function you have, tied to Python's __cmp__. It isn't an answer
              because it's rather too complicated for someone learning programming.

              import bisect

              def find(min = 1, max = 100):
              print "Think of a number between", min, "and", max
              a = range(min, max+1)
              try:
              bisect.bisect_l eft(a, Guess())
              print "Your number isn't between", min, "and", max
              print "Maybe you weren't thinking of an integer?"
              except Done, e:
              print "And it only took", e.count, "guesses"

              class Done(Exception) :
              def __init__(self, answer, count):
              self.answer = answer
              self.count = count

              class Guess:
              def __init__(self):
              self.count = 0
              def __cmp__(self, val):
              self.count = self.count + 1
              answer = raw_input("Is it %d? ([y] for yes, [<] for less than "
              "and [>] for greater than)? " % val)
              if answer == "y":
              raise Done(answer, self.count)
              if answer == "<":
              return -1
              return 1
              [color=blue][color=green][color=darkred]
              >>> from number_guess import find
              >>> find(1, 10)[/color][/color][/color]
              Think of a number between 1 and 10
              Is it 6? ([y] for yes, [<] for less than and [>] for greater than)? <
              Is it 3? ([y] for yes, [<] for less than and [>] for greater than)? y
              And it only took 2 guesses[color=blue][color=green][color=darkred]
              >>> find(1, 100)[/color][/color][/color]
              Think of a number between 1 and 100
              Is it 51? ([y] for yes, [<] for less than and [>] for greater than)? <
              Is it 26? ([y] for yes, [<] for less than and [>] for greater than)? >
              Is it 39? ([y] for yes, [<] for less than and [>] for greater than)? <
              Is it 33? ([y] for yes, [<] for less than and [>] for greater than)? >
              Is it 36? ([y] for yes, [<] for less than and [>] for greater than)? >
              Is it 38? ([y] for yes, [<] for less than and [>] for greater than)? y
              And it only took 6 guesses[color=blue][color=green][color=darkred]
              >>> find(1, 10)[/color][/color][/color]
              Think of a number between 1 and 10
              Is it 6? ([y] for yes, [<] for less than and [>] for greater than)? n
              Is it 9? ([y] for yes, [<] for less than and [>] for greater than)? <
              Is it 8? ([y] for yes, [<] for less than and [>] for greater than)? <
              Is it 7? ([y] for yes, [<] for less than and [>] for greater than)? <
              Your number isn't between 1 and 10
              Maybe you weren't thinking of an integer?[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Andrew
              dalke@dalkescie ntific.com


              Comment

              • Andrea Griffini

                #8
                Re: Guess My Number Game

                On Sun, 16 May 2004 14:57:28 GMT, "Andrew Dalke"
                <adalke@mindspr ing.com> wrote:
                [color=blue]
                >And not really an answer to the OP; here's a solution which uses
                >the bisect module, letting it do the heavy work. It uses the same sort
                >of guess function you have, tied to Python's __cmp__. It isn't an answer
                >because it's rather too complicated for someone learning programming.[/color]

                It also has the same problem about guessing too many times.

                bisect_left looks for a position in a sorted list given a value;
                the "guess the number" game is different, and you're looking
                for a value instead. You can come close by using a range as
                the artificial list, but you will end up guessing even when
                it's not needed (e.g. if answered "low" for 8 and "high" for 6
                you don't need another guess; you already know the answer is 7).

                It's not bisect_left fault, of course; that function is more
                general and can't take advantage from knowing that the list
                that is being passed is indeed a range of integers.

                It's also funny to see a bisection search called the "heavy
                work": the convoluted (and IMO somewhat ugly) code used to
                be able to call bisect is much more complex and less
                intuitive than the bisection code itself.

                To me seems it would be more accurate saying that the heavy
                work is trying to call bisect for a use that isn't what
                bisect was written for.

                Andrea

                Comment

                • Andrew Dalke

                  #9
                  Re: Guess My Number Game

                  Andrea Griffini:[color=blue]
                  > It also has the same problem about guessing too many times.[/color]

                  Mmm, I see your point. When I've played the game the last
                  one is supposed to be "then it must be 7". Here I finessed it
                  by assuming the player was a wit and chose a non-integer value.

                  With a bit more work I could make it test if numbers from
                  either side have been tested and abort early.
                  [color=blue]
                  > It's also funny to see a bisection search called the "heavy
                  > work": the convoluted (and IMO somewhat ugly) code used to
                  > be able to call bisect is much more complex and less
                  > intuitive than the bisection code itself.[/color]

                  Yeah, I was smiling too when I wrote it. It was more of a
                  "look at how leet my code is." :)

                  Andrew
                  dalke@dalkescie ntific.com


                  Comment

                  Working...