Beginner Exercises – Guess the number

Posted by : MOnsDaR | Sonntag, 22. August 2010 | Published in

I’m currently on the Python train again because a friend of mine wants to learn some basics about programming. Because its not easy to just learn the theoretical part without any  exercises I searched some beginner exercises for him.

In this series of articles I want to present some of the exercises along with a solution. The solutions will be written in Python, but the exercises could be written in any other language too.

So here is the first one:

Guess the number…

Found in an article from bazzy at cplusplus.com under its original name “Bracketing Search”

The program chooses a number between 0 and 100. The user then needs to guess that number. After finding the right one, a message will show the number of guesses the user needed. If the user guesses wrong, he will be told, if he was higher or lower than the number.

Example output:

Try to guess the secret number. It is in range 0 to 100.
Enter a number: 50
Too low...
Enter a number: 75
Too low...
Enter a number: 87
Too high...
Enter a number: 81
Too low...
Enter a number: 85
Too high...
Enter a number: 83
Got it, the number was 83
You needed 5 guesses to get it.
Press any key to continue . . .

Solution

(0) Comments

Leave a Response