431,795 Members | 1,250 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

# Guess My Number Game

 P: n/a 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? Jul 18 '05 #1
8 Replies

 P: n/a *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" schrieb im Newsbeitrag news:u9ppc.5447\$gr.390828@attbi_s52... 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? Jul 18 '05 #2

 P: n/a 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 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.randrange(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("Press enter to exit.") # --- End ----------------------------- // moma http://www.futuredesktop.org :: newbie links at the top Jul 18 '05 #3

 P: n/a 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 Jul 18 '05 #4

 P: n/a Am Samstag, 15. Mai 2004 15:44 schrieb EAS: 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: 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. Jul 18 '05 #5

 P: n/a On Sat, 15 May 2004 15:58:22 +0200, Heiko Wundram wrote: Not really python related, but... On average, the shortest possible runtime (and also a deterministic runtime ofO(log2(high-low))) will be achieved if you use interval walking.Thus:number = 64low = 1high = 100while 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 = medprint "I needed", tries, "tries." 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 Jul 18 '05 #6 