3 Replies - 2075 Views - Last Post: 25 July 2012 - 07:17 PM Rate Topic: -----

#1 godfrizzle23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • 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  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 godfrizzle23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 12-January 10

Re: Number guessing game

Posted 25 July 2012 - 04:40 AM

That's exactly it! Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1