Course 5.3Lesson 1 of 4
A guessing game is built by planning first. The computer picks a secret number with random.randint, the player has a limited number of tries, and each wrong guess gets the hint Too low or Too high. The thinking goes into two small functions, judge and score_for, that have no input or print, so they can be tested alone. The loop continues while there are tries left and the player has not won, and a flag variable remembers the win. Test the win on the first try, the win on the last try, and the loss.
Try the code and the simulations beside the lesson.
After this lesson you can
random.randint to pick a number,A good programmer thinks before typing. A program that you build without a plan grows in a strange shape, and a fault is hard to find. The plan of this game has two parts: the rules, and the expected answers.
The rules.
Too low or Too high.The expected answers. Before you write any code, work out some answers by hand. For a secret of 42: a guess of 30 is Too low, 50 is Too high, 42 is Correct. The score is 100 on the 1st try, 90 on the 2nd, 70 on the 4th, and 40 on the 7th.
Python has toolboxes of ready-made code. They are called modules. You bring one in with import. The module random has the function random.randint(1, 100), which gives a whole number from 1 to 100, both ends included. It is a different number every time you run the program.
Here is an idea that makes a program much easier to test. Do not mix thinking with asking and showing. The thinking is "how does this guess compare with the secret?" and "what is the score for this many tries?". The asking is input() and the showing is print(). We put each piece of thinking into a small function, with parameters and return, and no input() or print() inside.
This is a habit worth keeping: the part that thinks is a function. The part that talks to the user is the main program.
Because score_for() has no input() and no print(), it can be tested with a few calls before the game exists.
Now the main program uses the two functions.
Type some guesses in the Input box, one on each line, and press Run. Type more than you think you need. A game that asks for more guesses than the Input box has stops with an EOFError.
A game can end in three ways, and each needs a test. To test it, you can set secret = 42 instead of the random line for a moment.
| Guesses typed | What should happen |
|---|---|
42 | win on try 1, score 100 |
50, 30, 42 | win on try 3, score 80 |
1, 2, 3, 4, 5, 6, 42 | win on the last try, score 40 |
1, 2, 3, 4, 5, 6, 7 | loss, the secret is shown |
The third row is the edge: the winning guess is the seventh. An off-by-one fault shows there. Remember to put the random line back after the test.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
Very close when the guess is within 5 of the secret. Where does it go: in judge, or in the main program? (Hint: the thinking goes in judge.)Play again? (y/n).print inside the function that thinks. You cannot use its answer.input() into a number.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
input() or print(). Then you can test them alone.and. A flag remembers what happened.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
random.randint(1, 6) give?score_for(3)?judge better as a function without print?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.