Professional Documents
Culture Documents
Y10 02 CT9 Activities Solutions
Y10 02 CT9 Activities Solutions
If…elif…else, readability
Activity 1
An algorithm sorts packages into one of three gates based on weight according to this table.
Here are all the flowchart symbols needed to describe the algorithm. Arrange them so that
the algorithm will function correctly. Use each symbol only once. Use as many arrows as you
require.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
1
TOPIC 1: COMPUTATIONAL THINKING
If…elif…else, readability
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
2
TOPIC 1: COMPUTATIONAL THINKING
If…elif…else, readability
Activity 2
Here are the lines of code needed to translate the flowchart from Activity 1 into a functional
program. They are all jumbled up.
Copy or load the lines into your Integrated Development Environment (IDE). Rearrange the
lines to produce a functional program.
Remember, you will need to indent some lines. Be sure to use white space to make the code
more readable. Add two comments to explain the logic.
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
weight = 0.0 # Weight of the package
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
# Must be float because input can have decimals
weight = float (input ("Enter a weight in kilograms: "))
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
3
TOPIC 1: COMPUTATIONAL THINKING
If…elif…else, readability
Activity 3
Here is the flowchart for an algorithm that calculates the total cost of an order of
t-shirts. The price is based on the size and number ordered. Translate this flowchart into a
functional program using your IDE.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
4
TOPIC 1: COMPUTATIONAL THINKING
If…elif…else, readability
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
total = 0.0 # Total cost of order
number = 0 # Number of t-shirts
size = "" # Size needed
costSmall = 10.25 # Cost per size
costMedium = 12.88
costLarge = 15.25
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
number = int(input ("Enter a number of t-shirts: "))
size = input("Enter a size (S, M, L): ")
size = size.upper() # To match constants
if (size == "S"):
total = number * costSmall
elif (size == "M"):
total = number * costMedium
else:
total = number * costLarge
print("Your total order is " + str(total))
Activity 4
Using constants helps make logic clear and code more readable. The best constants to use
are ones that make sense in the context of a problem.
Here is a table of information that you will need to use in this activity.
Description Detail
Pi 3.14159
Circumference of a circle 2 πr
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
5
TOPIC 1: COMPUTATIONAL THINKING
If…elif…else, readability
Here are several code examples. Amend the code to define and use meaningful constants
(use the table above to help you). Write the amended code in the space below the original.
One has been done for you. Remember, constants are written in uppercase with
underscores.
PI = 3.14159
circumference = 2 * PI * radius
VAT_ZERO = 0.0
childShoeCost = total + (total * VAT_ZERO)
VAT_REDUCED = 0.05
childCarSeatCost = total + (total * VAT_REDUCED)
VAT_STANDARD = 0.20
Trainers = total + (total * VAT_STANDARD)
MAX_BUS_CAPACITY = 50
if (passengers > MAX_BUS_CAPACITY):
print ("Too many passengers.")
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
6
TOPIC 1: COMPUTATIONAL THINKING
If…elif…else, readability
Activity 5
Here is a set of labels for items that you would find in a program.
Here is a short program. Identify one instance of each item from the table and write the label
in the right place on the program.
If…elif…else, readability
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
8