Guess My Number Game

moma moma at example.net
Sat May 15 13:54:29 EDT 2004


EAS wrote:
> 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.randrange(101)
> print "The computer guessed", guess
> tries += 1
> past = guess
> while guess < number:
> guess = random.randrange(guess, 101)
> print "The computer guessed", guess
> tries += 1
> while guess > number:
> guess = random.randrange(0, guess)
> print "The computer guessed", guess
> tries += 1
> 
> print "The computer guessed your number after", tries, "tries."
> 
> raw_input("Press 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?
> 
> 

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("Press enter to exit.")

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


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






More information about the Python-list mailing list