Unit 8 - Practical Problem Solving and Programming From ICT G9 Student Book-2
Unit 8 - Practical Problem Solving and Programming From ICT G9 Student Book-2
Unit
and programming
Learning Objectives
By the end of Unit 8, learners should be able to:
• Draw flowcharts to solve simple problems
• Dry run flowcharts
• Write computer programs for simple problems
A flowchart is a diagram which uses symbols to represent problem solving steps. Lines and
arrows are used to show the sequences in which the steps occur. The shape of the symbol
indicates which type of information goes into the symbol.
Flowchart symbols
Symbols Description
Flowlines
177
Dry run flowcharts
A dry run is a technique which is used to test a flowchart by recording different values in a
trace table. It is possible to see what happens to these values as they follow the sequence of a
flowchart. This method can be used to check for errors which may exist in the flowchart logic.
A dry run can be done on paper by simply following a program flowchart, recording the values
which are used at each stage and writing the changes in a trace table.
Trace table
A trace table enables the recording of values (variables) which are used to dry-run the flowchart.
It also shows the output after these values have been processed.
Variable
Values must be stored in memory so that they can be processed. There are many locations in
the memory. A variable is a memory location which is given a symbolic name and the content
of the location can change during the execution of a program.
A variable can be pictured like a box which can hold a value ( text or number). A label (variable
name) is placed on the box.
Value which
is entered while
the program is being
executed
15
Age Age
178
UNIT 8 Practical problem solving and programming
INPUT Num1
The symbols
in the flowchart above SUM (Num1 + Num2)
are arranged in a specific
order which is known
as a Sequence.
OUTPUT SUM
STOP
The following sets of numbers will be used to dry run this flowchart.
Set 1: 8, 6.
Set 2: 3, 10.
Trace table:
Variables
8 6 8 + 6 = 14 14
Variables
Num1 Num2 SUM OUTPUT
Set 2
3 10 3 + 10 = 13 13
179
Activity 1
Try these values in the trace table above:
a) Set A: 58, 17
b Set B: 4, 7
Problem:
Create a flowchart which allows the user to enter a mark for an ICT test. If the mark is greater
than 49, then output Pass else output Fail.
Break down the problem into smaller steps which are easier to understand and solve.
This is called Step down refinement.
START
• INPUT: Mark
IF Marks
Yes
OUTPUT "Pass"
> 49?
No
OUTPUT "Fail"
A decision symbol
is used to show a
condition which may have
two possible outcomes. STOP
180
UNIT 8 Practical problem solving and programming
Modify the flowchart to allow the insertion of marks for five students.
No
OUTPUT "Fail"
IS Counter
= 5?
Yes
STOP
181
Activity 2: C
omplete the trace table below with the following marks:
10, 80, 45, 78, 60
List:
INPUT Temp
START
STOP
182
UNIT 8 Practical problem solving and programming
CREATE, CLOSE,
Used for processes
UPDATE, DELETE
Sequence
The order by which the steps occur. In structured English each line is
called a statement and they are executed from top to bottom.
Selection
A selection is used when a statement or a set of statements is executed
only if a certain condition is met.
Repetition/Iteration
It refers to a set of statements which are executed multiple times in the
program.
183
Introducing PYTHON
A program is a set of instructions that the computer executes. To create programs we use
special programs known as programming languages.
Programming refers to the action of writing program instructions. These instructions or lines
of codes are called program statements.
Python is an easy to use programming language. The Python software is free and it can be
downloaded from the following URL:
https://www.python.org/downloads/
On the Python web site you can choose the version which is the most appropriate for your
operating system.
184
UNIT 8 Practical problem solving and programming
With the help of your teacher download the Python installer, double click on its icon and follow
the instructions:
After installing the program, create a shortcut on the task bar to access it easily.
1. Click on
2. Click on
3. Move your pointer across the screen until you locate Python in the list.
Right click on the Python icon. Then click on Pin to taskbar.
To start Python, click on the icon which is found on the task bar.
185
The Python Screen
The 3 greater- than sign >>> is called the prompt. Commands are typed at the place which is
indicated by the prompt and the Enter key is used to execute the command.
Menu
Prompt
186
UNIT 8 Practical problem solving and programming
PYTHON Commands
The print command is used to output/display information. Type the command and strike Enter.
Displaying Strings/Characters
A string is a sequence of characters that the computer can process. Single quotes or double
quotes can be used. The quotes indicate to Python that what is inside the bracket is a string.
187
print (‘computer’) The program will output computer
Displaying Numbers
188
UNIT 8 Practical problem solving and programming
a) print (2+5)
b) print (10 – 8)
c) print (40 * 3)
d) print (100/4)
e) print (5+6-2)
189
Working with Variables John
Name = (‘John’)
Print (Name)
The program will output John the string which is found in the variable Name
Variable name
a) Age = 15
print (Age)
print ( Text )
190
UNIT 8 Practical problem solving and programming
The input () command allows you to read the data which is typed on the screen. This data is
then sent to a variable. Data which is stored in variables can be further processed or displayed.
Problem: Writing codes to allow the user to enter a Name in a variable and then output the
content of that variable.
Commands:
Name = input(‘Enter a name’)
Print (Name)
Steps: 1
191
Steps: 3 – Type the string Paul which will be sent to the variable Name.
Steps: 4 – Use the print command to display the content of the variable Name
192
UNIT 8 Practical problem solving and programming
Activity 6
rite Python statements to allow the user to enter a value in the variable Age and
a) W
then output its content.
Selection - IF STATEMENT
if expression :
print statement or statements for true
else:
print statement or statements for false
Consider the flowchart created above to check marks and output Pass if the mark is greater
than 49 else Fail if the mark is 49 or less.
193
Loop
Example: 1
To print a string a number of times. The while command can be used to do this.
while expression:
194
UNIT 8 Practical problem solving and programming
Example: 2
for expression:
To print a list of names, we use the for loop to print all the elements which are in the list one
after the other. The list contains 3 names John, Anne, Paul.
Variable or list containing the names. The for loop will take each item (x) in the
Remember to use ‘ ‘ for strings. list Names in turn.
195
Loops can also be used to do operations inside a string. For example, the variable Word contains
the string computer. The following codes display each character which makes up the string.
Variable containing the string ‘Computer’. For each value of (x) in the string ‘computer’.
196
UNIT 8 Practical problem solving and programming
The range function returns a sequence of numbers starting from 0 which is the default value.
output
Statement which defines
the range for the loop.
Print (x) will cause the program to print
numbers in the range.
Activity 7
a) W
rite Python statements to allow the user to input an Age and print “Adult” if the
age is greater than 21 and “Youngster” if the age is 20 or less.
b) Write Python statements to allow the user to input his name and display it five times.
c) W
rite Python statements to allow the user to input his name and output each
alphabets separately.
197
END OF UNIT QUESTIONS
1. Fill in the blanks in the sentences below by using the following keywords:
2. a) Complete the flowchart by using the words from the list below.
START
List:
Yes IF Age
No
> 19?
STOP
198
END OF UNIT QUESTIONS UNIT 8 Practical problem solving and programming
b) Complete the trace table for the above flow chart with the following year of birth:
2000, 1998, 2005, 2010
199
Additional Notes
200
UNIT 8 Practical problem solving and programming
Additional Notes
201