473,242 Members | 1,461 Online

# 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 1272
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 5 by: Nathan Pinno | last post by: Hi all, What's wrong with the following code? It says there is name error, that random is not defined. How do I fix it? # Plays the guessing game higher or lower. # Originally written by Josh... 5 by: Nicolae Fieraru | last post by: Hi All, I use VC .Net 2003 and I want to create a small program which is able to read the source code of a web site. I have a textbox with the URL, a button and a multiline textbox for the... 7 by: harish.mallipeddi | last post by: Hi all, This might sound a bit weird but anyways here I go. Recently after witnessing the popularity of AJAX/DHTML, and after enjoying Gmail's fairly cool UI, I'm left wondering...is there going... 11 by: Lues | last post by: Hi, I'm trying to protect some data in tables with encription (you know why, don't you ;)) I must confess that I'm not very expirienced in writing code, especially encription code. Can any... 232 by: robert maas, see http://tinyurl.com/uh3t | last post by: I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first... 4 by: moopower | last post by: I have to do the following: For this project, you are to create a simple number guessing game. The program should begin by asking the user for a range (i.e. a minimum number and a maximum number)... 1 by: Gomi | last post by: Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of... 2 by: tesa | last post by: I am not able to figure out how to make this work. I am trying to create a hangman game. I am in a basic javascripting class. I am to only use very basic code as you can see. I am able to use any... 0 by: Dan Upton | last post by: On Wed, May 21, 2008 at 12:42 PM, garywood