[Tutor] number game
Jonathon Sisson
sisson.j at gmail.com
Tue Oct 3 20:57:30 CEST 2006
Mike,
The algorithm you use is what causes the infinite loop (it cycles
through the same numbers repeatedly). I've updated the code and I'll
post it here:
def num(number):
r=input("range >")
ran=range(r+1)
guess=r/2
print guess
guesses=1
min = 0
max = r
while guess!=number:
if guess < number:
min = guess
else:
max = guess
guess = (min + max) / 2
guesses += 1
print guess
print "i got the number",number,"in",guesses,"guesses"
mike viceano wrote:
> i wrote a program that guesses number in the most effective way i could
> think of but i keep running into endless loops any help fixing it would be
> great :)
>
> here it is:
>
>
> def num(number):
> r=input("range >")
> ran=range(r+1)
> guess=r/2
> print guess
> guesses=1
> while guess!=number:
> if guess < number:
> guess= ((r-guess)/2)+guess
> print guess
> guesses=guesses+1
> elif guess > number:
> guess= ((r-guess)/2)-guess
> print guess
> guesses=guesses+1
> print "i got the number",number,"in",guesses,"guesses"
>
>
>
> my only idea is to make it so it dosint re guess numbers but i would rather
> just find out what is wrong
> and fix it
>
> _________________________________________________________________
> Express yourself - download free Windows Live Messenger themes!
> http://clk.atdmt.com/MSN/go/msnnkwme0020000001msn/direct/01/?href=http://imagine-msn.com/themes/vibe/default.aspx?locale=en-us&source=hmtagline
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list