# Number guessing game

Page 1 of 1

## 3 Replies - 2371 Views - Last Post: 25 July 2012 - 07:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286853&amp;s=1749937760aa3e3c877df9396f71e79a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 godfrizzle23

Reputation: -1
• Posts: 28
• Joined: 12-January 10

# Number guessing game

Posted 24 July 2012 - 10:15 PM

So, I've been trying to work out a program to have python guess a number that I choose. I have most of it down, but the issue comes from python guessing the same number more than once. I want it to act like a real person guessing a random number until they get the answer. What I'm not certain how to do would be to have it still guess a random number, but omit numbers that it's already guessed. Here's what I have:

```import random

theNumber = int(input("Give me a number to guess between 1-100: "))
minNumber = 1
maxNumber = 100
guess = random.randint(minNumber, maxNumber)
tries = 1

print("Is it", guess, "?")

while guess != theNumber:
if guess > theNumber:
tries += 1
maxNumber = guess
guess = random.randint(minNumber, maxNumber)
if guess == theNumber:
break
print("No wait, it's", guess)
elif guess < theNumber:
tries += 1
minNumber = guess
guess = random.randint(minNumber, maxNumber)
if guess == theNumber:
break
print("No wait, it's", guess)

print("\n\nHa, the number is", theNumber, "isn't it?")
print("It only took me", tries, "tries!")

```

Any suggestions as to how I can go about achieving this? Thanks!

Is This A Good Question/Topic? 0

## Replies To: Number guessing game

### #2 atraub

• Pythoneer

Reputation: 813
• Posts: 2,192
• Joined: 23-December 08

## Re: Number guessing game

Posted 24 July 2012 - 10:43 PM

that's easy enough.

secret number:30
max guess = 100
min guess = 1

first guess: 64
Too high!
maxguess = 63 #it can't be 64, right?
minguess = 1

second guess: 15
too low!
max guess = 63
min guess = 16

You already have the right idea, but you're not adding or subtracting one from the guess when you update max and min. If you do that, there's no way for the pc to guess the same number more than once.

### #3 godfrizzle23

Reputation: -1
• Posts: 28
• Joined: 12-January 10

## Re: Number guessing game

Posted 25 July 2012 - 04:40 AM

That's exactly it! Thanks!

### #4 carnivroar

• D.I.C Regular

Reputation: 29
• Posts: 387
• Joined: 18-September 11

## Re: Number guessing game

Posted 25 July 2012 - 07:17 PM

randint(a, b ) returns a random integer between a and b... including a and b! So you'd have to remove those as such:

```import random

minNumber = 1
maxNumber = 100
while(True): # makes sure user doesn't input number outside range
theNumber = int(input("Give me a number to guess between 1-100: "))
if (theNumber >= minNumber and theNumber <= maxNumber):
break
guess = random.randint(minNumber, maxNumber)
tries = 1

print("Is it", guess, "?")

while guess != theNumber:
if guess > theNumber:
maxNumber = guess-1 #<-----
else:
minNumber = guess+1 #<-----
tries += 1
guess = random.randint(minNumber, maxNumber)
if guess == theNumber:
break
print("No wait, it's", guess)

print("\n\nHa, the number is", theNumber, "isn't it?")
print("It only took me", tries, "tries!")

```

Also I simplified your code a bit.

This post has been edited by carnivroar: 25 July 2012 - 07:26 PM