Lesson 37: Safe moves
Check before you move
- Data can be wrong: too big, below zero, missing, or a word instead of a number.
- Validation is a check the computer does before it uses the data.
- Bad data is rejected with a message; good data is used.
Words for this lesson
| Word | Meaning |
|---|---|
| range check | the value is between a lowest and a highest value |
| presence check | something was entered (it is not empty) |
| type check | the value is the right data type, e.g. a whole number |
| test data | values chosen to test the checks |
Test data for "1 to 500"
| Kind | Example | Result |
|---|---|---|
| normal | 300 | accepted |
| extreme | 500 | accepted (the highest allowed) |
| boundary | 500 and 501 | one accepted, one rejected |
| abnormal | -50, ten | rejected |
In the exam
- 0478/22/F/M/25 Q4: (a) the purpose of a presence check. Mark scheme: to check that a value has been entered. (b)(i) the check that the input is an integer: a type check.
- 0478/21/M/J/23 Q3(b): test data for "an integer between 30 and 200 inclusive". Mark scheme: normal 75 (in range, accepted); abnormal Sixty (wrong data type, rejected); extreme 200 (the highest value, accepted). Give a reason for each.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
The robot gets a list of distances to drive. Some are not safe. Before each move, measure the space in front (the program takes 100 mm off to be safe). Add a range check: drive only if the distance is more than 0 및 not more than the space, then print OK and the distance. Otherwise print Rejected and the distance. The wall is in a different place on each map.
출력을 보려면 '실행'을 클릭하세요.
난이도 4/5 · 280코인 + 오늘 첫 완료 시 작은 보너스 추가
Now the distances arrive as text, like typed input. Some are empty and one is a word. int("ten") would crash the program, so check first. Presence check: if the text is empty, print Missing. Type check: if it is not all digits (r.isdigit() is False), print Not a number: and the text. Only then turn it into a number and do the range check.
출력을 보려면 '실행'을 클릭하세요.