473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Guess My Number Game

EAS
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 12003
*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" <er****@attbi.nospam.com> 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
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
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
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
On Sat, 15 May 2004 15:58:22 +0200, Heiko Wundram <he*****@ceosg.de>
wrote:

Not really python related, but...
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."


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
Andrea Griffini
Not really python related, but...
On average, the shortest possible runtime (and also a deterministic runtime of
O(log2(high-low))) will be achieved if you use interval walking.
... supposing that guess(x) returns -1, 0 or 1 depending
if the number is too low, correct or too high.


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_left(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
from number_guess import find
find(1, 10) 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 find(1, 100) 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 find(1, 10) 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?


Andrew
da***@dalkescientific.com
Jul 18 '05 #7
On Sun, 16 May 2004 14:57:28 GMT, "Andrew Dalke"
<ad****@mindspring.com> wrote:
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.


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
Jul 18 '05 #8
Andrea Griffini:
It also has the same problem about guessing too many times.
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.
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.


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

Andrew
da***@dalkescientific.com
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RootShell | last post by:
Anyone knows where i can find a FREE PHP GAME script which is based on the genre "Name The Game" where a picture of a game is shown and the user has to guess which game it is by a mean of 5...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
5
by: tigrfire | last post by:
So I'm trying to write a hangman game and the output is coming out a little strange. Here's my code thus far: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> ...
3
by: cathf | last post by:
I am working on a program where the user guesses a random number generated in VB. The numbers entered by the user must be within the range 1 to 20, so the program has been validated. All valid...
7
by: Suudsu2200 | last post by:
Hi I am making a basic "guess what number I'm thinking of" script and I cant figure out what I'm doing wrong. When I enter my number it changes it to the right number and says correct. (p.s. kinda...
9
by: cdm2883 | last post by:
Ok im trying to make a lil random number game. I have most of the code and comments on how i want the game to work. but i can not get to it work. if you have any answers any help what so ever it woul...
2
by: realNewbie | last post by:
I am trying to duplicate the Guess My Number Game from Python Programming for the Absolute Beginner by Michael Dawson but for some reason I still cannot figure out my output is not completely...
7
by: dseto200 | last post by:
I'm able to create a guessing number game, but I can limit the amount of guesses to less than 3. Can someone tell me what i'm doing wrong? # Guess My Number # The computer picks a random number ...
3
by: zehra5 | last post by:
function rnd(scale) { var dd=new Date(); return((Math.round(Math.abs(Math.sin(dd.getTime()))*8.71*scale)%scale)); }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.