0% found this document useful (0 votes)
2 views18 pages

L4 Writing Algorithms

The document provides an overview of writing algorithms in pseudocode and introduces programming concepts using Python. It covers types of programming errors, including syntax, run-time, and logic errors, along with examples and exercises. Additionally, it emphasizes the differences between pseudocode and actual code, and the importance of planning algorithms before coding.

Uploaded by

Zohaib Afridi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

L4 Writing Algorithms

The document provides an overview of writing algorithms in pseudocode and introduces programming concepts using Python. It covers types of programming errors, including syntax, run-time, and logic errors, along with examples and exercises. Additionally, it emphasizes the differences between pseudocode and actual code, and the importance of planning algorithms before coding.

Uploaded by

Zohaib Afridi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Objectives

• Be able to write algorithms in pseudocode


• Identify the different types of programming errors
and how to fix them:
• syntax errors
• run-time errors
• logic errors
Writing algorithms
Introduction to Python

Starter
• What does the following algorithm do?
#GhostHunter
if ghost catches player then
lives  lives – 1
endif
if lives = 0 then
display "Game Over!"
else
display "Arghhh!"
endif
Writing algorithms
Introduction to Python

Starter
#GhostHunter
if ghost catches player then
lives  lives – 1 If the ghost catches a player
endif then a life is lost
if lives = 0 then If the player has no lives left
display "Game Over!" then game over is shown

else
display "Arghhh!" Otherwise, the text ‘Arghhh’ is shown
endif
Writing algorithms
Introduction to Python

Pseudocode
• Pseudocode is an informal language used to
describe the steps of algorithms
• It cannot be directly run, but a programmer will be
able to understand how the algorithm works and then
program the algorithm
• It is useful for planning an algorithm before you
attempt to code it
Writing algorithms
Introduction to Python

Pseudocode styles
• The following may be used when writing
pseudocode:
• English words or phrases may be used if it is obvious to a
programmer how to implement them
•  may be used for assignment instead of = e.g.
maxPlayers  2
• Normal Maths symbols are okay to use such as     
• Alternative words could be used, such as OUTPUT or
DISPLAY instead of print
• Programming constructs such as IF, THEN, ELSE are used
Writing algorithms
Introduction to Python

From Pseudocode to Python


• The program on the right is written in INPUT num1
pseudocode
INPUT num2
• Work with a partner to translate it into a
useable program in Python total  num1 + num2

• Do this exercise on paper average  total  2


OUTPUT average

Pseudocode
Writing algorithms
Introduction to Python

From Pseudocode to Python


INPUT num1 num1 = int(input("Enter first number:
"))
INPUT num2
num2 = int(input("Enter second number:
total  num1 + num2 "))

average  total  2 total = num1 + num2

OUTPUT average average = total / 2


print("The average is: ", average)
Pseudocode Python
Writing algorithms
Introduction to Python

Worksheet 4
• Complete Task 1 on Worksheet 4
Writing algorithms
Introduction to Python

Syntax errors
• Syntax errors are where the code prnt("Menu")
you have written has a mistake print("----")
against the rules of the language
print("1. Book appointment)
• It is like a grammar mistake in English
print("2. Cancel apointment")
• Find the three syntax errors in the code
print("3. Exit")
choice = int(input("Enter choice")
Writing algorithms
Introduction to Python

Syntax errors
prnt is not defined prnt("Menu")
(it should be print)
print("----")
Missing end quote " print("1. Book appointment)
print("2. Cancel apointment")
print("3. Exit")
Missing end bracket ) choice = int(input("Enter choice")

• Note that the spelling of appointment


in line 4 is not a syntax error
• The program will still run
Writing algorithms
Introduction to Python

Run-time errors
• Run-time errors occur whilst the
program is running
• Your computer has limited RAM.
If you tried to put more data into RAM
than you have space for you would get
a = 50
an ‘Out of memory’ run-time error b = 0
• Another type of run-time error is when
a program attempts to divide a number result = a / b
by zero
ZeroDivisionError:
• Try dividing by zero on a calculator division by zero
and see what happens
Writing algorithms
Introduction to Python

Logic errors
• Some programs run perfectly a = 50
but they don’t work as the b = 100
programmer intended
total = a - b
• They contain logic errors
• The example shown runs in
Python but it doesn’t
work correctly
• What is the logic error in the
program on the right?
Writing algorithms
Introduction to Python

Logic errors
• What is the logic error in the a = 50
program on the right? b = 100
• The third line should add the two numbers
to make a total total = a + b
• If a minus were used, the program will run,
but it won’t give the correct answer
Writing algorithms
Introduction to Python

Worksheet 4
• Now complete Task 2 on Worksheet 4
Writing algorithms
Introduction to Python

Plenary
• Work with a partner to:
• Give three ways that the style of
pseudocode may differ from actual code
• Give three different types of error that
computer programs may contain
Writing algorithms
Introduction to Python

Plenary
• Pseudocode differences
• English words or phrases may be used
•  used instead of = for assignment
• Use of Maths symbols such as     
• Alternative words such as OUTPUT or DISPLAY

• Give three different types of error that computer


programs may contain
• Run-time error
• Syntax error
• Logic error
Writing algorithms
Introduction to Python

Copyright

© 2023 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by
you only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part
of the materials distributed with this unit may be used, reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, electronic or otherwise, without the prior written permission of
PG Online Limited.

Licence agreement

This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the
worksheets, PowerPoint presentations, teaching guides and other associated files distributed with it is
licensed, not sold, to you by PG Online Limited for use under the terms of the licence.

The materials distributed with this unit may be freely copied and used by members of a single institution
on a single site only. You are not permitted to share in any way any of the materials or part of the
materials with any third party, including users on another site or individuals who are members of a
separate institution. You acknowledge that the materials must remain with you, the licencing institution,
and no part of the materials may be transferred to another institution. You also agree not to procure,
authorise, encourage, facilitate or enable any third party to reproduce these materials in whole or in part
without the prior permission of PG Online Limited.

You might also like