AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Planning and testing code

Week 3 · Unit 7.1: Block it out · 3 lessons
1

Plan first, then code

Computing 7: Unit 7.1, pp. 8–43
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.

StartINPUT rowsINPUT per_rowplants = rows * per_rowOUTPUT "Plants:", plantsEnd
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.
Steprowsper_rowplants
INPUT rows4––
INPUT per_row46–
plants = rows * per_row4624
OUTPUTshows 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 symbolPython line
INPUT ageage = int(input("Age? "))
total = a + btotal = a + b
OUTPUT totalprint(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.
StartINPUT metrescentimetres = metres * 100OUTPUT centimetresEnd
2
Write this flowchart as a Python program. The inputs are whole numbers.
StartINPUT start_kmINPUT end_kmdistance = end_km - start_kmOUTPUT "Distance:", distanceEnd
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.
4
The flowchart has a mistake in the order of its steps. Describe the mistake and draw the correct order.
StartOUTPUT totaltotal = red + greenINPUT redINPUT greenEnd