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? 8 11748
*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?
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
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
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(highlow))) 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.
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(highlow))) 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 = t1
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
Andrea Griffini Not really python related, but...
On average, the shortest possible runtime (and also a deterministic
runtime ofO(log2(highlow))) 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
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
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 noninteger 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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by RootShell 
last post: by

138 posts
views
Thread by theodp 
last post: by

5 posts
views
Thread by tigrfire 
last post: by
                