Programming simple problems in Python
In a linear algorithm all commands are carried out one after another from top to bottom: no condition is checked and nothing is repeated. A simple task is solved in four stages: 1) find the input data, its type and its name; 2) find what the result is and which type it will have; 3) build the algorithm: input, calculation, output; 4) test it with different values. In a flowchart the oval shows the start and the end, the parallelogram input and output, and the rectangle a calculation; each of them matches one or several lines of code. For example the input block matches an input() line, the calculation block an assignment line, and the output block print(). The fourth stage matters most: by trying an example whose answer is known in advance you see that the algorithm works correctly.
| Block | Lines of code |
|---|---|
| input | input() |
| calculation | assignment (v = s / t) |
| output | print() |
A frame ribbon was needed for a rectangular poster on a classroom wall. A pupil made a worksheet taking width and length: data, result (perimeter), algorithm, test. The four orderly stages kept mistakes few.
Engineers, accountants and programmers solve a simple problem in four stages and check the answer.
Write four stages on paper for another small problem, such as the area of a circle: data, result, formula, test.
1s = int(input()) 2t = int(input()) 3v = s / t 4print(v)
60.0
1a = int(input()) 2n = int(input()) 3m = int(input()) 4print(m - a * n)
6500
“From chart to code”: the group draws a flowchart on the board for the volume of a cube (oval – parallelogram – rectangle – parallelogram – oval), then writes the matching line of code under each block.