Course 4.2Lesson 3 of 5
A loop can ask the user again and again until the answer is right. A guessing game starts with a value that is not the secret, so that the loop begins, asks inside the loop, counts the guesses, and gives a hint. To check an answer, use while True with a break when the answer is good. A sentinel such as 0 ends a list of numbers. On this site, the answers go into the Input box before Run, one on each line, and a loop that asks many times needs as many lines as it asks, or the program stops with an EOFError.
Try the code and the simulations beside the lesson.
After this lesson you can
A loop can ask a question again and again. The classic example is a number guessing game.
Type these three guesses in the Input box, one on each line, and run it: 50, 30, 42. The program answers Too high, Too low and Correct! You needed 3 guesses. Look at the game.
guess = 0 makes a first value that is not the secret, so that the loop starts.while guess != secret: repeats while the guess is wrong. (!= means not equal.)False, and the loop ends.Remember from Chapter 4: on this site, the answers go into the Input box before you press Run. A loop that asks many times needs as many lines as it asks. If you type too few, Python stops with an EOFError, which means that the answers ran out. Type more guesses, or guess better.
On your own computer, the program simply waits for each guess.
A program should not trust what the user types. If you ask for an age, the user may type 200 or -5. A while True loop can ask again.
Type 200 on the first line and 15 on the second line of the Input box, and run it. The program says that 200 is not possible, asks again, and accepts 15. The loop has the form: ask, check, break if good, otherwise show a message and go round again. This is one of the most useful patterns in programming.
An important point. If the user types a word like abc, then int() stops with a ValueError. We shall learn to catch such an error in the next book. For now, the loop checks the range of numbers.
How can the user tell the program that there are no more numbers? A sentinel is a special value, such as 0, that means stop.
Type 5, 10, 20 and 0, one on each line, and run it. The program shows that you gave 3 numbers and that their total is 35. Look at the shape of the loop. The first input() is before the loop, and the second is the last line of the body. This is so that the condition number != 0 always looks at a fresh number.
Run this code with only three lines in the Input box, and leave out the 0. The loop asks once more and gets an EOFError.
| Job | Shape |
|---|---|
| ask until the answer is right | while guess != secret: and ask inside |
| ask until the answer is acceptable | while True: ask, then break if good |
| read numbers until a sentinel | ask before the loop, test, and ask again at the end of the body |
guess. The loop never starts, or there is a NameError.EOFError.input() into a number. "50" != 42 is always True.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:
while True with break is a natural way to check an answer.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
guess = 0 before the guessing loop?EOFError mean here?input() both before the loop and at the end of the body?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.