0% found this document useful (0 votes)
249 views

Python Worksheet 4 Writing Algorithms

Uploaded by

Paxton Nyeo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views

Python Worksheet 4 Writing Algorithms

Uploaded by

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

Worksheet 4 Writing algorithms

Introduction to Python

Worksheet 4: Writing algorithms


Task 1
Here is some sample pseudocode. Use this as a guide to write pseudocode algorithms for
the descriptions in the questions below.

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

Input “enter student’s mark:” to student_mark


If student_mark >+ 40 then
Print “pass”
Else
print”fail”
Endif
end

2. To print the correct hat size based on the circumference of your head:

 Less than 57cm = Small


 Greater than 60cm = Large
 Anything in between = Medium

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

3. For a program that will:

 Read in the price of an item and the quantity purchased.


 Calculate the total spend.
 If the total spend is £50 or more, the customer will be given a discount of 10%.
Calculate the discount if due.
 If applicable, display a message saying they qualify for a discount and the discount
amount.
 Output the amount to pay, (deduct any discount due if applicable to the total spend)

Here is an example where discount is applicable


Item Price: £6, quantity purchased: 10
Total Spend : £60
Discount : £6 (10% of £60)
Amount to pay : £54
START

INPUT "Enter the price of the item: £" TO ITEM_PRICE


INPUT "Enter the quantity purchased: " TO QUANTITY

TOTAL_SPEND = ITEM_PRICE * QUANTITY

IF TOTAL_SPEND >= 50 THEN


DISCOUNT = 0.10 * TOTAL_SPEND
PRINT "You qualify for a discount of £" + DISCOUNT
TOTAL_SPEND = TOTAL_SPEND - DISCOUNT
ELSE
DISCOUNT = 0
ENDIF

PRINT "Amount to pay: £" + TOTAL_SPEND

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.

 What type of errors are these?


 Can you identify the errors and correct the code?

 month = Month.title(): Month should be month because Python is case-


sensitive.
 if month == "December" or month = "January" or month == "February"::
The second condition uses a single equals sign = (assignment operator) instead
of the double equals sign == (comparison operator).
 Print(month,"is in Autumn"): Print should be lowercase as print.

After Correction:

month = input("Enter the month of year: ")


month = month.title()
if month == "December" or month == "January" or month == "February":
print(month, "is in Winter")
elif month == "March" or month == "April" or month == "May":
print(month, "is in Spring")
elif month == "June" or month == "July" or month == "August":
print(month, "is in Summer")
elif month == "September" or month == "October" or month == "November":
print(month, "is in Autumn")
else:
print("Check spelling of month.")

input("Press ENTER to quit")

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

Example of tax calculation:

Salary
Tax rate Example salary Tax calculation
bracket

<=30,000 20% 25,000 25,000 *20% = 5,000

20% on first 30,000 30,000 x 20% = 6,000


> 30,000 100,000 70,000 x 40% = 28,000
40% for remaining
total tax = 34,000

 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:

salary = int(input("Please enter your annual salary: £"))

if salary <= 30000:


# Salaries under 30000 are taxed at 20%
tax = salary * 0.2
else:
# Salaries over 30000 are taxed at 40% for anything over 30000
tax_over_30k = salary - 30000
tax = 30000 * 0.2 + tax_over_30k * 0.4

print("Earnings of £", salary, "will attract taxes of £", round(tax, 2))


input("Press ENTER to quit")

4
Worksheet 4 Writing algorithms
Introduction to Python

Three types of errors

 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)

You might also like