Course 3.1Lesson 4 of 5
A decision must be tested at every branch, and especially at the edges, where one condition changes into the next, because that is where most mistakes hide. Make a test table: the input, the answer that you expect from the rules, and the answer of the program. Test one value inside each branch and the values on both sides of each edge, such as 49, 50 and 51. The usual bugs are the wrong order, a one-off edge between < and <=, a missing else, and = instead of ==. A dry run, following the program by hand, finds them.
Try the code and the simulations beside the lesson.
After this lesson you can
A decision program can look right and still be wrong for one input. The program works for the examples that you tried, and fails for the next. A test tries the program on values that you have chosen on purpose, with answers that you know before you run it.
A test table has three columns: the input, the answer that you expect (worked out from the rules, not from the program), and the answer that the program gives. For the grade ladder, the rules are: 90 and above is A, 75 to 89 is B, 50 to 74 is C, and below 50 is D.
| Marks | Expected | The program gives | Right? |
|---|---|---|---|
| 0 | D | ||
| 49 | D | ||
| 50 | C | ||
| 74 | C | ||
| 75 | B | ||
| 89 | B | ||
| 90 | A | ||
| 100 | A |
Fill in the third column by running the program, and compare it with the second.
You cannot test every number. So choose well. For each decision, test
49, 50, 51, then 74, 75, 76, then 89, 90, 91. Most mistakes hide at an edge.This is only about twenty values, and it catches nearly every mistake.
A test at 70 or at 80 cannot find this bug. Only the test at the edge itself, 75, shows it.
marks > 75 where you meant >= 75: the value 75 gets the wrong grade. Only a test at 75 shows it.else. There is a gap: some values fall through every condition, and the variable is never set. Python then stops with a NameError, or the old value stays.= instead of ==. A SyntaxError, which at least is easy to see.When a test fails, follow the program by hand for that input, like a trace table. Write the value of the variable, and for each condition write True or False.
For marks 75 and the wrong program if marks > 90 ... elif marks > 75 ...:
| Check | Result |
|---|---|
marks > 90 | False |
marks > 75 | False (75 is not more than 75) |
marks > 50 | True, so the grade is C |
The expected grade was B. We see the mistake: > should be >=. The dry run found it in one minute.
Before you write a decision, write the rules in words and the test table. After you write the program, run the tests. If a test fails, the fault is more often in the program than in the test, but check the table too.
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:
else, and = for ==.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
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.