Writing algorithms
Introduction to Python
Learning objectives
• Learn to write algorithms in pseudocode
• Review the difference between syntax errors, run-
time errors and logic errors
• Learn techniques for debugging programs
Introduction to Python
Writing algorithms
What does this algorithm do?
#GhostHunter
if ghost catches man then
lives lives – 1
if lives = 0 then
display "Game Over!"
else
display "Arghhh!"
Introduction to Python
Writing algorithms
Writing Pseudocode
• Pseudocode is half code, half English
• It has no real syntax and would not run
• It is a structured sequence of instructions
• It is useful for planning algorithms
Use it to plan before coding!
Introduction to Python
Writing algorithms
Suggested language for
Pseudocode
• Use indentation in pseudocode
• Use words such as:
print, display, input, output, calculate, add,
set, reset, if, then, else
Introduction to Python
Writing algorithms
Pseudocode Example:
if unlock button pressed then
display PIN unlock screen
input PIN
if correct PIN entered
then
unlock phone
else display “Try again”
else display lock screen
Introduction to Python
Writing algorithms
Writing an Algorithm
• Write a pseudocode algorithm to display the correct
hat size based on the circumference of your head:
Tips
Less than 57cm = Small
Greater than 60cm = Large You will need to use:
Anything in between = Medium
if
elif
else
And the operators < less than
and >more than
Introduction to Python
Writing algorithms
Now write the code…
• You should understand the problem more clearly
• It should be easier to write – in any language!
There is a video online to help
Introduction to Python
Writing algorithms
Types of programming error
• Syntax Errors
• Run-Time Errors
• Logic Errors
Introduction to Python
Writing algorithms
Debugging exercise
• Look at the two programs below and see if you can
find and correct the errors in them
SeasonFinderBUGS.py
TaxCalculatorBUGS.py