GoalRead a flowchart with input, process and output symbols, trace it with a trace table and write it as a Python program.
New words
algorithm · a precise list of steps that solves a problemflowchart · a diagram that shows the steps of an algorithm with symbols and arrowsprocess · a step where the program works something out or stores a valuetrace table · a table that shows the value of each variable after every step
Explanation
Programmers plan an algorithm before they type code, and a flowchart is a good plan. An oval means start or stop. A parallelogram means input or output. A rectangle means a process, such as a calculation. Arrows show the order. Each symbol turns into one or two lines of Python. An INPUT becomes an input line with a cast, a process becomes an assignment, and an OUTPUT becomes a print line. A plan helps you break the problem into small steps, so you only have to get the syntax right.
Worked examples
Python
1rows = int(input("Rows? "))
2per_row = int(input("Plants in each row? "))
3plants = rows * per_row
4print("Plants:", plants)
Output
Plants: 24
A school garden has rows of seedlings. This is the Python program for the garden flowchart. Each line matches one symbol. The user types 4 and then 6.
Step
rows
per_row
plants
INPUT rows
4
–
–
INPUT per_row
4
6
–
plants = rows * per_row
4
6
24
OUTPUT
shows Plants: 24
To check a flowchart, trace it with a trace table. Write the variables across the top and fill in a row after each step. We use rows = 4 and per_row = 6. A dash means that the variable has no value yet.
Flowchart symbol
Python line
INPUT age
age = int(input("Age? "))
total = a + b
total = a + b
OUTPUT total
print(total)
Each symbol has a typical Python line. Learn the pattern and you can translate any simple flowchart.
Class activity
Shape cards (no computer): cut four paper ovals, parallelograms and rectangles, or draw them on cards. A partner reads a program one line at a time, for example hours = int(input()), minutes = hours * 60, print(minutes). You choose the right shape card for each line, write the words on it and lay the cards in a column with arrow lines drawn between them. Then trace the cards with the number 3 for hours, and say what is output. Swap roles and use your own program. If you have a computer or tablet, draw the same flowchart in a drawing program.
Practice
1
Trace the flowchart when the user enters 4. Then trace it when the user enters 12.
For 4 it outputs 400, because 4 * 100 = 400. For 12 it outputs 1200.
2
Write this flowchart as a Python program. The inputs are whole numbers.
Line 1: start_km = int(input("Start? ")). Line 2: end_km = int(input("End? ")). Line 3: distance = end_km - start_km. Line 4: print("Distance:", distance).
3
Plan a program on paper. It must ask for a number of weeks and show how many days that is. Draw the flowchart and use the right shape for every step.
Start (oval), INPUT weeks (parallelogram), days = weeks * 7 (rectangle), OUTPUT days (parallelogram), Stop (oval), with arrows between them.
4
The flowchart has a mistake in the order of its steps. Describe the mistake and draw the correct order.
The output comes before the inputs and the calculation, so the program would show a total that has not been worked out yet. The correct order is: INPUT red, INPUT green, total = red + green, OUTPUT total.