Course 2.1Lesson 3 of 5
A person may type a letter where you want a number, or an age of minus five. A good program asks again. The recipe has three parts: a while True loop that keeps asking, a try and except that catch a bad conversion, and a break or a return that leaves the loop when the answer is good. A second check handles a good number in a bad range. Put the loop in a function with parameters for the question and the limits, so that it can be used again. On this site, the bad answers go first in the Input box and the good answer last.
Try the code and the simulations beside the lesson.
After this lesson you can
A person may type a letter where you want a number, or -5 where you want an age. A good program does not crash. It asks again.
The recipe has three parts:
while True: loop that keeps asking.try and except that catch a bad conversion.break (or a return) that leaves the loop when the answer is good.Predict first. Type abc, then 200, then 14 in the Input box, one on each line. What will the program say each time? Then run it.
abc makes int(text) fail. The except ValueError part says to type a number, and continue goes back to the top of the loop to ask again. The lines below the try are skipped.200 converts without trouble, but fails the range test. The if part says what is wrong, and the loop goes round again.14 is good, so break leaves the loop.There are two different checks in the loop: the try checks the type (can it be a whole number?), and the if checks the range (is it from 0 to 120?). Never mix them up. The try cannot test a range, and the if cannot test whether the text is a number.
All the answers are typed in the Input box before you press Run, in the order in which they are asked. Put the bad ones first and the good one last. If you give too few lines, the program stops with an EOFError, because the answers ran out.
You will ask for numbers in many programs. Instead of copying the loop, put it in a function, with the question and the limits as parameters.
Type x, 200, 14, -3 and 75, one on each line, and run it. The function asks for the age until it gets 14, and then the mark until it gets 75.
In the real program this check sits inside while True, so the question comes back until the answer passes both checks.
Is it good to ask for ever? For a game or a form, yes. For a program that others run in a script, with no user, an endless loop is a trap. A real tool often allows a number of tries, or a way to quit by typing q. Think about who will use your program.
continue after the except. The program goes on with a number that was never made, and a NameError follows."abc" < 0 is a TypeError.and where or is needed: a bad age is age < 0 or age > 120.EOFError.int("12.5") is a ValueError too, and 12.5 is refused. If decimals are welcome, convert with float().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:
None.while True, try, except and break (or return) make a loop that asks until the answer is good.try checks the type. if checks the range.continue goes back to the top of the loop.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
int("abc") give?try and an if in the loop?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.