Guess The Number

January 17, 2012

We previously wrote a program to provide small exercises in arithmetic for children just learning their basic facts. Today’s exercise is similar; it plays a guessing game where children compete against the computer to see which can guess the number faster. Here’s a sample dialog:

Let's play Guess-The-Number!
You go first.
I am thinking of a number from 1 to 100
What is your guess? 38
My number is less than your guess.
What is your next guess? 15
Your guess is less than my number.
What is your next guess? 25
Your guess is less than my number.
What is your next guess? 33
My number is less than your guess.
What is your next guess? 28
Your guess is less than my number.
What is your next guess? 31
Your guess is less than my number.
What is your next guess? 32
You guessed my number 32 in 6 tries!
Now it's my turn.
Think of a number from 1 to 100.
Is your number less than 50? Yes
Is your number less than 25? No
Is your number less than 37? Yes
Is your number less than 31? No
Is your number less than 34? Yes
Is your number less than 32? No
Is your number less than 33? Yes
I guessed your number 32 in 7 tries.
You made less guesses than me.
You win! Congratulations!
Would you like to play again? No
Thanks for playing! Good-bye!

Your task is to write a program that plays Guess-The-Number; the maximum number in play (100 in the sample dialog) should be a parameter to the program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

About these ads

Pages: 1 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 609 other followers

%d bloggers like this: