Python Worksheet 4 Writing Algorithms
Python Worksheet 4 Writing Algorithms
Introduction to Python
input PIN
if correct PIN entered then
unlock phone
else
print "Try again"
endif
1. Read in a student’s mark and print ‘pass’ or ‘fail’ depending on their mark. The pass
mark is 40 or more.
Start
2. To print the correct hat size based on the circumference of your head:
Start
Input “enter the circumference of your head incm:” to head_circumference
if head_circumference <57 then
Print “hat size: small”
elseif head_circumference >60 then
1
Worksheet 4 Writing algorithms
Introduction to Python
Print “hat size: large”
Else
Print “hat size: medium”
Endif
end
END
2
Worksheet 4 Writing algorithms
Introduction to Python
Task 2
For this task you need to access the programs SeasonFinderBUGS.py and
TaxCalculatorBUGS.py. Both programs have errors in them.
1. SeasonFinderBUGS.py is a program that takes in the month of the year and outputs
the season that month occurs in eg input → January, Output → Winter.
There are four errors in the code.
After Correction:
3
Worksheet 4 Writing algorithms
Introduction to Python
2. L4 WS4 TaxCalculatorBUGS.py is a tax calculator program that will calculate how
much tax is to be paid by an individual. The rules are:
Salary is 30,000 or less, tax is 20% of salary
Salary is over 30,000, tax is 20% for first 30,000 and 40% for anything over
Salary
Tax rate Example salary Tax calculation
bracket
The program has five errors in it. Can you identify and correct the errors?
Which of the three types of errors you have learnt about in the lesson is NOT found
in this program?
The last else block (which is meant for salaries over 150,000) is redundant and
doesn't match the rules provided, so it should be removed.
The line salary = salary - 30000 is deducting 30,000 from the salary, which
changes the original salary value. Instead of changing the salary variable, use
another variable for this operation.
Typo in print: salray should be salary.
The program doesn't explicitly handle cases when the salary is exactly 30,000.
The calculation for salaries over 30,000 is incorrect. It deducts 6,000 (20% of
30,000) but the correct deduction should be the full 30,000 * 20%.
Corrected code:
4
Worksheet 4 Writing algorithms
Introduction to Python
Syntax errors: These are mistakes in the structure of the code, such as missing
colons or mismatched parentheses. (Present in original code)
Runtime errors: These errors occur during the execution of the program and can
result in the program crashing. (Not observed in original code)
Logical errors: Mistakes in the logic that lead to incorrect outputs. (Present in
original code)