473,405 Members | 2,404 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,405 software developers and data experts.

Newb ??

Hi all, I am new to the group. Trying to learn Python programming on my
own. I am working through Michael Dawson's Book Python Programming for the
absolute beginner.

I am tring to write a program that is a number guessing game. I want to be
able to give the user 5 tries to guess the number before the program ends.

I get the result that I want when the user misses after the 5th try. But it
does not go down to my closing statement to end. Rather is askes for
another guess.

I appreciate any help you can give. If this is not the correct group for
these types of questions, I apologize and hope that you can direct me to
another NG.

Thanks,
Chad

import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")
Nov 9 '05 #1
20 2002

Chad Everett wrote:
Hi all, I am new to the group. Trying to learn Python programming on my
own. I am working through Michael Dawson's Book Python Programming for the
absolute beginner.

I am tring to write a program that is a number guessing game. I want to be
able to give the user 5 tries to guess the number before the program ends.

I get the result that I want when the user misses after the 5th try. But it
does not go down to my closing statement to end. Rather is askes for
another guess.
Because even after 5 tries, guess != the_number. You won't get past
the while loop until it does. When there have been 5 misses, try
setting
guess=the_number.

I appreciate any help you can give. If this is not the correct group for
these types of questions, I apologize and hope that you can direct me to
another NG.

Thanks,
Chad

import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")


Nov 9 '05 #2

me********@aol.com wrote:
Chad Everett wrote:
Hi all, I am new to the group. Trying to learn Python programming on my
own. I am working through Michael Dawson's Book Python Programming for the
absolute beginner.

I am tring to write a program that is a number guessing game. I want to be
able to give the user 5 tries to guess the number before the program ends.

I get the result that I want when the user misses after the 5th try. But it
does not go down to my closing statement to end. Rather is askes for
another guess.


Because even after 5 tries, guess != the_number. You won't get past
the while loop until it does. When there have been 5 misses, try
setting
guess=the_number.

I appreciate any help you can give. If this is not the correct group for
these types of questions, I apologize and hope that you can direct me to
another NG.

Thanks,
Chad

import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")


here is your corrected version:
##FROM here it's good
import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
## Here you always get 100, randrange is from start to end+1
the_number = random.randrange(0,101)
##This is OK
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
## Missing break to explicitly leave loop after 5
break
## Then test to see if last guess equals the_number
if guess != the_number:
print "Sorry you lose."
print "The Correct Number was ", the_number
else:
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")

Nov 9 '05 #3
Chad Everett wrote:
Hi all, I am new to the group. Trying to learn Python programming on my
own. I am working through Michael Dawson's Book Python Programming for the
absolute beginner.

I am tring to write a program that is a number guessing game. I want to be
able to give the user 5 tries to guess the number before the program ends.

I get the result that I want when the user misses after the 5th try. But it
does not go down to my closing statement to end. Rather is askes for
another guess.

I appreciate any help you can give. If this is not the correct group for
these types of questions, I apologize and hope that you can direct me to
another NG.

Thanks,
Chad

import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")

I don't personally think the two possible solutions you've been given
are very pythonic. All you really need to do to get the loop termination
conditions right (he says confidently, and with absolutely no testing at
all) is change the while line to

while guess != the_number and tries <=5:

but there are other things wrong with the program. In "pseudo-code"
(stuff that helps you think about the problem without being able to feed
it into an interpreter) your solution should be

while not guessed and guesses < 5
guess number
if guessed
congratulate user
else
crow and rub user's face in it

At present your code will always tell the user she guessed correctly,
even after it's told her she lost. See if this (tested) rewrite makes
sense to you:
import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while guess != the_number and tries < 5:
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1

if guess == the_number:
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"
else:
print "Sorry you lose."
print "The Correct Number was ", the_number

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 9 '05 #4
I would rather to do the loop as :

max_try = 5
for x in xrange(max_try):
g = int(raw_input(["Take a guess","Take another guess"][x != 0]))
if g == number:
print "bingo, hit it in %i tries" % x + 1
break
elif g < number: print "try higher"
else: print "try lower"
else: print "sorry, you lose, the number is %i" % number

Chad Everett wrote:
import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")


Nov 9 '05 #5

## Here you always get 100...

This is false , sorry for the wrong comment on this part, it should
rather be:
## randrange is from start to end-1
the_number = random.randrange(1,101)
JM
(But the rest of my comment seems OK)

Nov 9 '05 #6
:
> Hi all, I am new to the group. Trying to learn Python programming on my
> own. I am working through Michael Dawson's Book Python Programming for the
> absolute beginner.
>
> I am tring to write a program that is a number guessing game. I want to be
> able to give the user 5 tries to guess the number before the program ends.


< snip >

I am also working my way slowly through this book which I find quite
challenging. What I am thinking about at the moment is how to program for
the computer to guess the number which I select. I know it can be done but
I suspect it requires an approach which I have not yet learnt. I would
welcome suggestions on the best way to approach this problem.

Norman
Nov 9 '05 #7
Hey guys,

Thanks for your help. I am glad to know there is a community out there
willing to put the effort out to help us newbies. I will work on it. Some
of the suggestions are things that I have not covered yet but I am sure I
will figure it out.

Thanks again.

I am sure I will be back with more questions before too long.

Chad
"Chad Everett" <ev*******@hotmail.com> wrote in message
news:mz***************@bignews2.bellsouth.net...
Hi all, I am new to the group. Trying to learn Python programming on my
own. I am working through Michael Dawson's Book Python Programming for
the absolute beginner.

I am tring to write a program that is a number guessing game. I want to
be able to give the user 5 tries to guess the number before the program
ends.

I get the result that I want when the user misses after the 5th try. But
it does not go down to my closing statement to end. Rather is askes for
another guess.

I appreciate any help you can give. If this is not the correct group for
these types of questions, I apologize and hope that you can direct me to
another NG.

Thanks,
Chad

import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")

Nov 10 '05 #8
Norman Silverstone <no****@littletank.org> wrote:
...
challenging. What I am thinking about at the moment is how to program for
the computer to guess the number which I select. I know it can be done but
I suspect it requires an approach which I have not yet learnt. I would
welcome suggestions on the best way to approach this problem.


I assume the way the computer is going to guess is by trying some
number, and you respond either that it's guessed right, or to go lower,
or to go higher.

In that case, think of "bisection". Originally, all the computer knows
is that the number is in some range, say 0 to 100. It can then guess
the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
each case the range was just halved (actually, a bit more than halved).

It does not take many halvings to squeeze even a pretty large original
range down to a range which only includes one possible number... that's
because "2 to the power of N" grows VERY fast with N, as the old story
about the inventor of chess shows (the king told him to ask for a
reward, and all he wanted was some rice -- one grain on the first cell
of the chessboard, two on the next, then four, eight... all the way
through the 64 squares of the board; the kind thought the inventor was
being very moderate in his request, and granted it... to soon find out
that not enough rice existed in the world to satisfy the request...;-).

Well, repeated halving is just like repeated doubling "backwards", so it
squeezes vast ranges of possibilities down to tiny ones just as fast as
repeated doubling produces oceans of rice from a small chessboard;-).
Alex
Nov 10 '05 #9
< snip>
I assume the way the computer is going to guess is by trying some
number, and you respond either that it's guessed right, or to go lower,
or to go higher.
Yes, that is correct.
In that case, think of "bisection". Originally, all the computer knows
is that the number is in some range, say 0 to 100. It can then guess
the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
each case the range was just halved (actually, a bit more than halved).


Thank you, I thought that might be the case. So, I will have to settle
down and try to write some pseudo-code first. I hope to be back.

Norman
Nov 10 '05 #10
On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:
In that case, think of "bisection". Originally, all the computer knows
is that the number is in some range, say 0 to 100. It can then guess
the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
each case the range was just halved (actually, a bit more than halved).


Thank you, I thought that might be the case. So, I will have to settle
down and try to write some pseudo-code first. I hope to be back.


Heh, you will find that Python is practically executable pseudo-code!

Untested:
def guess_number():
# please don't cheat the poor computer...
print "Guess a number."
lo = 0
hi = 100
while True:
guess = (lo+hi)//2
ans = raw_input("Is it %d? y/n " % guess)
if ans in ('y', 'yes'):
break
ans = raw_input("Too high? y/n ")
if ans in ("y", "yes"):
hi = guess-1
else:
lo = guess+1

This should run, and it will *almost* do what you want.
--
Steven.

Nov 10 '05 #11
On Fri, 11 Nov 2005 01:39:40 +1100, Steven D'Aprano wrote:
On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:
In that case, think of "bisection". Originally, all the computer knows
is that the number is in some range, say 0 to 100. It can then guess
the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
each case the range was just halved (actually, a bit more than halved).


Thank you, I thought that might be the case. So, I will have to settle
down and try to write some pseudo-code first. I hope to be back.


Heh, you will find that Python is practically executable pseudo-code!

Untested:
def guess_number():
# please don't cheat the poor computer...
print "Guess a number."
lo = 0
hi = 100
while True:
guess = (lo+hi)//2
ans = raw_input("Is it %d? y/n " % guess)
if ans in ('y', 'yes'):
break
ans = raw_input("Too high? y/n ")
if ans in ("y", "yes"):
hi = guess-1
else:
lo = guess+1

This should run, and it will *almost* do what you want.


Thanks for that but I think it is too simplistic. It appears OK for the
first guess, which is 50 but, what about the next guess. If the guess is
too high then the next guess has to be 50/2. However, if it is too low
then the next guess must be first guess + (100-second guess)/2. In general
terms, if guess is too high then next guess must (guess - lowest
possible)/2 and if too low then it is guess + (highest possible -
guess)/2.

Comments please.

Norman

Nov 10 '05 #12
Norman Silverstone wrote:
On Fri, 11 Nov 2005 01:39:40 +1100, Steven D'Aprano wrote:

On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:

In that case, think of "bisection". Originally, all the computer knows
is that the number is in some range, say 0 to 100. It can then guess
the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
each case the range was just halved (actually, a bit more than halved).

Thank you, I thought that might be the case. So, I will have to settle
down and try to write some pseudo-code first. I hope to be back.


Heh, you will find that Python is practically executable pseudo-code!

Untested:
def guess_number():
# please don't cheat the poor computer...
print "Guess a number."
lo = 0
hi = 100
while True:
guess = (lo+hi)//2
ans = raw_input("Is it %d? y/n " % guess)
if ans in ('y', 'yes'):
break
ans = raw_input("Too high? y/n ")
if ans in ("y", "yes"):
hi = guess-1
else:
lo = guess+1

This should run, and it will *almost* do what you want.

Thanks for that but I think it is too simplistic. It appears OK for the
first guess, which is 50 but, what about the next guess. If the guess is
too high then the next guess has to be 50/2. However, if it is too low
then the next guess must be first guess + (100-second guess)/2. In general
terms, if guess is too high then next guess must (guess - lowest
possible)/2 and if too low then it is guess + (highest possible -
guess)/2.

Comments please.

Norman

Effectively you want to start with a minposs and maxposs, which are set
to 0 and 100 respectively. Your guess should bisect the range (as nearly
as it can given that you are dealing with integers, and you have to be
careful to get the awkward boundary conditions right). So your first
guess will be 50. If your guess is too low then replace minposs with
your guess (in this case 50); if too high, replace maxposs with your
guess; loop to generate the next guess, and so on. In practice since
log2(100) > 5 your five guesses won't always be enough (though seven
should be).

Hope this helps.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 10 '05 #13
Norman Silverstone wrote:
Heh, you will find that Python is practically executable pseudo-code!

Untested:
def guess_number():
# please don't cheat the poor computer...
print "Guess a number."
lo = 0
hi = 100
while True:
guess = (lo+hi)//2
ans = raw_input("Is it %d? y/n " % guess)
if ans in ('y', 'yes'):
break
ans = raw_input("Too high? y/n ")
if ans in ("y", "yes"):
hi = guess-1
else:
lo = guess+1

This should run, and it will *almost* do what you want.
Thanks for that but I think it is too simplistic. It appears OK for the
first guess, which is 50 but, what about the next guess.


did you test the script?
If the guess is too high then the next guess has to be 50/2. However, if it is too low
then the next guess must be first guess + (100-second guess)/2. In general
terms, if guess is too high then next guess must (guess - lowest
possible)/2 and if too low then it is guess + (highest possible -
guess)/2.

Comments please.

Norman

--
http://mail.python.org/mailman/listinfo/python-list


Nov 10 '05 #14
Norman Silverstone wrote:
Heh, you will find that Python is practically executable pseudo-code!

Untested:
def guess_number():
# please don't cheat the poor computer...
print "Guess a number."
lo = 0
hi = 100
while True:
guess = (lo+hi)//2
ans = raw_input("Is it %d? y/n " % guess)
if ans in ('y', 'yes'):
break
ans = raw_input("Too high? y/n ")
if ans in ("y", "yes"):
hi = guess-1
else:
lo = guess+1

This should run, and it will *almost* do what you want.
Thanks for that but I think it is too simplistic. It appears OK for the
first guess, which is 50 but, what about the next guess.


did you test the script? here's a simulator:

import re

number = 0
guess = None
count = 0

def raw_input(prompt):
global count, guess
reply = "n"
m = re.search("\d+", prompt)
if m:
# guess a number
count = count + 1
guess = int(m.group())
if guess == number:
reply = "y"
else:
# check if too high
if guess > number:
reply = "y"
print prompt, reply
return reply

def guess_number():
# please don't cheat the poor computer...
print "Guess a number."
lo = 0
hi = 100
while True:
guess = (lo+hi)//2
ans = raw_input("Is it %d? y/n " % guess)
if ans in ('y', 'yes'):
break
ans = raw_input("Too high? y/n ")
if ans in ("y", "yes"):
hi = guess-1
else:
lo = guess+1

for number in range(0, 100+1):
print "NUMBER IS", number
count = 0
guess_number()
print "SUCCESS AFTER", count, "GUESSES"

# end
Comments please.


if this had been a java "let's pretend you're the java runtime" certification
question, you would have failed.

</F>

Nov 10 '05 #15
On Thu, 10 Nov 2005 17:00:38 +0000, Norman Silverstone
<no****@littletank.org> declaimed the following in comp.lang.python:
Thanks for that but I think it is too simplistic. It appears OK for the
first guess, which is 50 but, what about the next guess. If the guess is
too high then the next guess has to be 50/2. However, if it is too low
then the next guess must be first guess + (100-second guess)/2. In general
terms, if guess is too high then next guess must (guess - lowest
possible)/2 and if too low then it is guess + (highest possible -
guess)/2.
I'm having trouble following that -- I think you may be complicating
things more than you need to...

The basic algorithm only needs to know the "current" low and high
end of the range.

new_guess = (current_low + current_high) / 2

Match: break out of loop, with a successful guess
Low: current_low = new_guess; repeat #could be new_guess +1
#as you know it can not
#be "new_guess"
High: current_high = new_guess; repeat #or new_guess-1
Say the number is 78
low high guess result
0 100 (0+100)/2 => 50 LOW
50 100 (50+100)/2 => 75 LOW
75 100 (75+100)/2 => 88 HIGH (rounded up)
75 88 (75+88)/2 => 82 HIGH (rounded up)
75 82 (75+82)/2 => 79 HIGH (rounded up)
75 79 (75+79)/2 => 77 LOW
77 79 (77+79)/2 => 78 MATCH

NOTE: if you use the +1/-1 adjustments (to eliminate the guess
itself from being an end-point, as you know it can not be that value)
the above will converge to a match one step sooner.
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Nov 10 '05 #16
On Thu, 10 Nov 2005 17:31:18 +0000, Steve Holden wrote:
Effectively you want to start with a minposs and maxposs, which are set
to 0 and 100 respectively. Your guess should bisect the range (as nearly
as it can given that you are dealing with integers, and you have to be
careful to get the awkward boundary conditions right). So your first
guess will be 50. If your guess is too low then replace minposs with
your guess (in this case 50); if too high, replace maxposs with your
guess; loop to generate the next guess, and so on. In practice since
log2(100) > 5 your five guesses won't always be enough (though seven
should be).


Dude, that's what my code does, although I admit I took zero care to get
the awkward boundary conditions right, nor did I put code in to stop the
game after five attempts.

--
Steven.

Nov 10 '05 #17
Steven D'Aprano wrote:
Dude, that's what my code does, although I admit I took zero care to get
the awkward boundary conditions right, nor did I put code in to stop the
game after five attempts.


as my simulator shows, your code needs 5.87 attempts to make a correct
guess, on average.

if you add a 5-guess limit, it will find the right number only in 30% of all
runs. here's a complete list of (max number of guesses; % chance that
you'll find the right answer given an even distribution of target numbers)

1 1.0
2 3.0
3 6.9
4 14.9
5 30.7
6 62.4
7 100.0

fwiw, you may improve things slightly by special-casing the first guess:

http://www.pomona.edu/Magazine/pcmfl00/1.shtml

</F>

Nov 10 '05 #18
did you test the script? here's a simulator:
< snip>

Fredrik, thank you very much indeed for taking the trouble to show me the
way. I am sorry that I made the comment I did, that will teach me to read
more carefully. It is said that there is no fool like an old fool and, as
I am approaching 78 years old, I think I qualify. It is also said that you
are never too old to learn so I am trying.

Now, I put the script you gave into an editor and ran it , (I use Ubuntu
Linux by the way). It certainly showed how the computer arrived at the
number guessed but guessed the number itself and gave me no chance to say
whether high or low. I noticed also that the numbers were all greater than
50 and the loop ran until the number guessed was 100, then it stopped.
Perhaps you can point out to me how I should go about debugging.

Incidentally, I am only just starting to learn about functions and have
not come across the module 're'. Also why is it (lo+hi)//2 and not
(lo+hi)/2.

Thanks again for your help.

Norman

# end
Comments please.


if this had been a java "let's pretend you're the java runtime"
certification question, you would have failed.

</F>


Nov 11 '05 #19
Norman Silverstone <no****@littletank.org> wrote:
...
Incidentally, I am only just starting to learn about functions and have
not come across the module 're'. Also why is it (lo+hi)//2 and not
(lo+hi)/2.


Using // ensures truncation, which is what you want. A single / may mean
truncating division in the default legacy/compatibility mode, but with
the new division behavior (which one day will become standard) it means
true division, so that for example 1/2 means 0.5 -- better get used to
the new behavior ASAP (which is why you can ask for new division
behavior with -Qnew on Python's commandline or 'from __future__ import
division' at the top of your module -- to help you get used to it).
Alex
Nov 11 '05 #20
Norman Silverstone wrote:
did you test the script? here's a simulator:


< snip>

Fredrik, thank you very much indeed for taking the trouble to show me the
way. I am sorry that I made the comment I did, that will teach me to read
more carefully. It is said that there is no fool like an old fool and, as
I am approaching 78 years old, I think I qualify. It is also said that you
are never too old to learn so I am trying.

Now, I put the script you gave into an editor and ran it , (I use Ubuntu
Linux by the way). It certainly showed how the computer arrived at the
number guessed but guessed the number itself and gave me no chance to say
whether high or low. I noticed also that the numbers were all greater than
50 and the loop ran until the number guessed was 100, then it stopped.
Perhaps you can point out to me how I should go about debugging.


debugging? the script I posted was intended to show you that given an
honest human (simulated by the raw_input function), the posted algorithm
found the right answer for all values in the given range. (if the algorithm
had been broken, the simulator wouldn't have finished).

if you want to play yourself, use Steven's original code, and follow the in-
structions...

</F>

Nov 11 '05 #21

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

Similar topics

0
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to...
4
by: Hari | last post by:
Basically I would like to downlod the visual basic 6.0 compiler, but i already have the vb.net compiler. I had to pay for the VB.net IDE, just wondering if I can get the vb 6.0 IDE for free or not....
0
by: David E. | last post by:
So as a programmer, what's the best thing to study? EJB? How much of the J2EE or Enterprise architecture is necessary to no? I guess I need a good overview for a newb like me... thanks.. --...
5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
3
by: Walter | last post by:
But I'm stumped..... I've got a windows 2000 server and I am trying to set up PHPBB on it using a mysql database.. I am very inexperienced on this..... Ive installed mysql V4.0.20d and I can...
3
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to...
1
by: notbob | last post by:
Newb here! Using 4.0.20 on Slack. Slogging through the official manual. At 2.4.3 Securing the Initial MySQL Accounts, I'm finally stopped cold while trying to follow instructions. Here's what I...
4
by: Donald Newcomb | last post by:
I'm a real Python NEWB and am intrigued by some of Python's features, so I'm starting to write code to do some things to see how it works. So far I really like the lists and dictionaries since I...
6
by: Sean Berry | last post by:
Hello all I have build a list that contains data in the form below -- simplified for question -- myList = ,, ...] I have a function which takes value3 from the lists above and returns...
11
by: The_Kingpin | last post by:
Hi all, I'm new to C programming and looking for some help. I have a homework project to do and could use every tips, advises, code sample and references I can get. Here's what I need to do....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.