Beginner Exercises – Patient Game

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

Another one from Bazzy on cplusplus.com. Its original name was “While( user == gullible )”.

The program asks the user to enter any number but a special one. If the user finally enters the special number, a message will put out, praising the user if he was very patient. (If the user has done the game more than 10 times)

Example output:

Do NOT enter 1: 2
Do NOT enter 2: 3
Do NOT enter 3: 4
Do NOT enter 4: 5
Do NOT enter 5: 6
Do NOT enter 6: 7
Do NOT enter 7: 8
Do NOT enter 8: 9
Do NOT enter 9: 0
Do NOT enter 10: 11
Do NOT enter 11: 11

Wow, you were very patient.
You did the game 11 times

Solution

Beginner Exercises – Guess the number

Posted by : MOnsDaR | | 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