471,354 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

code for number guessing by computer

hi, please help me,,,,,, a newbie.......send me code for a programme
in which the computer tell u the number that u guessed............

Sep 30 '07 #1
1 1207
hi, please help me,,,,,, a newbie.......send me code for a programme
in which the computer tell u the number that u guessed............
I can think of at least two versions of this:
>>_ = raw_input("Think of an integer greater than 1 and less
than 3")
>>print "Your number was 2! This code is teh awesome!"
or, if you need to guess an arbitrary number greater than "N",
you can use this spiffy code:
>>print "Think of an integer greater than zero and I will guess it"
i = 1
while raw_input("Was your number %i?" % i) <"yes": i += 1
....
>>print "I guessed your number correctly!"

Oh...unless this was a homework problem and you were looking for
a standard binary search of a problem-space, in which case,
mailing lists don't generally do your homework for you.
However, the first solution I provided is *far* better than a
boring old binary-search because it solves the problem in O(1)
rather than O(log N) which any good math wonk will tell you is a
much more efficient algorithm! :)

-tkc

Sep 30 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nathan Pinno | last post: by
5 posts views Thread by Nicolae Fieraru | last post: by
7 posts views Thread by harish.mallipeddi | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
4 posts views Thread by moopower | last post: by
reply views Thread by Dan Upton | last post: by

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.