Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
The secret number is 42, the limit is 7 tries, and the player types 50, 30, 36, 42. Write a trace table with one row for each round, with the columns tries, the guess, what judge says, and won. Then say what the program shows at the end.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Follow the loop by hand. Before the loop, tries is 0 and won is False. The condition is tries < 7 and not won.
| Round | tries after the count | guess | judge says | won after |
|---|---|---|---|---|
| 1 | 1 | 50 | Too high | False |
| 2 | 2 | 30 | Too low | False |
| 3 | 3 | 36 | Too low | False |
| 4 | 4 | 42 | Correct | True |
After round 4, not won is False, so the loop stops. The program shows the hints Too high, Too low and Too low, then the end message.
The score is score_for(4), which is 100 - 10 * 3 = 70.
Check. Run the game with the secret set to 42 and these four guesses, and compare.
Answer: The program shows Too high, Too low, Too low, and then You found it in 4 guesses. Score: 70.
Log in to join the discussion.
Loading…