Grade 8 ICT Unit 1 Recap
Chatbots
Data types
Data type
Example
String
Any text more than 1 symbol like “Hello world!” or “Teacher
Rouxan”
Character
Any text of a single symbol like “L” or “@”
Integer
Any whole number like 24 or -10
Read/Float
Any number with a decimal point like 4.2 or -9.1
Boolean
True or False values (to a statement) like 10 > 2 → False
How do we get a user (someone) to type information into your program?
You use an input function:
name = input(“What is your name? “)
print(name)
A variable is a name that contains some data.
Variable: name
Data type: string
Python thinks any input is text unless you tell it otherwise: Like we will input 5 and 3 as string
Code
Output
When text is added together like 5 and 3 to be 53 we call it concatenation.
To tell your computer the input will be integers/floats/boolean/etc. we have to do casting.
When you use an input function the program always thinks you are putting “words” or a
string in as data.
-
If you want to input an integer: casting the input as an integer
number = int(input(“What is your favourite number? “))
print(number)
-
If you want to input a float: casting the input as a float
number = float(input(“What is your height? “))
print(number)
So let’s do casting to change the values from string to integers
Code
Output
Naming Conventions for Variables
The name you give a variable is important because it tells you what you are storing:
name = “David”
name → variable
data → string
Two main ways of naming a variable:
1. camelCase
all lowercase except for the first letter of the second word
Example: favouriteNumber = int(input(“What’s your favourite number? “))
2. snake_case
all lowercase but separated by underscore _
Example: favourite_number = int(input(“What’s your favourite number? “))
Developing in iteration
When writing programs just like with science or anything else we have a certain way we do
it:
We follow this iterative process:
1. Requirements: Ask what is needed for the program/product to function.
2. Design: Create the algorithm (the steps the computer will follow) with a flowchart or
pseudocode; then check the pseudocode with a tracetable.
3. Develop: The program code is written.
4. Test: A structured test plan is used to test if the program is functioning correctly.
5. Review:
1. Fix any mistakes identified from the test plan
2. Write down possible improvements for the next iteration
3. Determine if the program meets the requirements from the start
4. Decide if you need to iterate again
6. Launch: Release the final program.
Showing algorithms using flowcharts
Subroutine
Example of Python code versus Flowchart:
Python code:
num = int(input(“Enter a number”))
if num > 0:
print(“Greater than 0”)
else:
pass
A set of predefined
code
Code Tracers
A trace table is a structured approach like a test plan that can look at an algorithm (or
program code) and follow the variables and conditions.
Look at the structure of a trace table:
Example: Trace table
Error Processing
What are conditional operators in conditional
statements?
For more examples on trace tables:
ICT Student Textbook
Pg. 25 - 27
Sequence: (normal code - line for line)
A series of program instructions.
Selection: (If-else statement)
Iteration: (A loop)
A count-controlled loop is where a series of program instructions is repeated a set number
of times. Also called a for loop.
Flowcharts of iteration
Iteration as a flowchart and pseudocode
Iteration as code and pseudocode
Types of common mistakes
Sub-Routine in flowcharts and code:
Code:
def addition(x,y):
c=x+y
print(c)
def subtraction(a,b):
z = a-b
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
operation = int(input("Enter 1 for addition or 2 for subtraction: "))
if operation == 1:
addition(number1,number2)
elif operation == 2:
subtract(number1,number2)
else:
print("OUT Enter a valid number”)
Iteration and arrays
An array is a data structure that can store more than one item of data under a single
identifier (name). In Python an array can hold only one data type, such as
list = [“cat”, “dog”, “mouse”].
Or another example:
How do you add to an array?
You can use the append method. Here is an example of it:
Code
Output
How to use a for loop in an array. Here is an example of it:
Code
Output
How to count the number of items in an array (list). Here is an example
of it: