Idea 20of 23Builds onFind the bug, fix the line
It runs, but is the math right?
Can code run perfectly and still give a wrong answer?
Today you'll discover
- why running is not the same as right
- how to check an answer makes sense
- how to fix the math
Think first4 tickets at $6 each: what do they cost?
The code ran with no problem and gave $10. What is wrong?
Decide first, then press Next. Estimate the answer yourself before you trust the code.
Ticket code on the left: SET 6, ADD 4, OUTPUT, giving $10 beside a goal of $24. The fixed code on the right: SET 6, MULTIPLY BY 4, OUTPUT, the changed line framed, giving $24, which meets the goal. The last step shows garden code that starts at SET 5 and adds 5 three times, giving 20 plants.
See the whole ideathe big idea and the key word, in one place
The big idea
Code that runs without stopping can still be wrong, so check the answer is reasonable.
Compare themRuns: the computer could do every line. Right: the answer fits the situation. Good code needs both.
Easy to get wrongIf the output is not reasonable, rethink the math first, then change the code.
Key word
- reasonable
- makes sense for the situation, such as being close to an estimate
Ready to use it?
- Decide whether a program's output makes sense, and fix the math.