Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

TOPIC 1: COMPUTATIONAL THINKING

If…elif…else, readability

ACTIVITIES SOLUTIONS FOR Y10-02-CT9

Activity 1
An algorithm sorts packages into one of three gates based on weight according to this table.

Weight (kilograms) Gate

0.1 to 1.99 Green

2.0 to 4.99 Yellow

5.0 or greater Red

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
# ------------------------------------------------------------

# =====> Arrange these lines to make the code work


print("Open Green gate.")
print("Open Red gate.")
print("Open Yellow gate.")
else:
if (weight >= 5.0):
elif (weight >= 2.0):
weight = float(input("Enter a weight in kilograms: "))
weight = 0.0

# ------------------------------------------------------------
# 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: "))

if (weight >= 5.0):


print ("Open Red gate.")
elif (weight >= 2.0):
print ("Open Yellow gate.")
else:
print ("Open Green gate.") # Default option

© 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

Passengers allowed on a bus 50

Value-added tax (VAT) on almost all goods Standard rate of 20%

Value-added tax (VAT) on children’s clothing


Zero rate of 0%
and footwear

Value-added tax (VAT) on child car seats Reduced rate of 5%

© 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.

circumference = 2 * 3.14159 * radius

PI = 3.14159
circumference = 2 * PI * radius

childShoeCost = total + (total * 0.0)

VAT_ZERO = 0.0
childShoeCost = total + (total * VAT_ZERO)

childCarSeatCost = total + (total * 0.05)

VAT_REDUCED = 0.05
childCarSeatCost = total + (total * VAT_REDUCED)

trainers = total + (total * .020)

VAT_STANDARD = 0.20
Trainers = total + (total * VAT_STANDARD)

if (passengers > 50):


print("Too many passengers.")

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.

Comment Assignment Selection statement

Built-in function (not a string


Constant Initialisation
function)

Variable String function Indentation

An integer variable A string variable A real variable

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.

Comment Assignment Selection statement


© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
7
TOPIC 1: COMPUTATIONAL THINKING

If…elif…else, readability

Line 11 Lines 1-5, 7-9, 12-14, 19, 22 Lines 16-22

Constant Initialisation Built-in function (not a string


function)
SMALL, MEDIUM, LARGE, Lines 1-5, 7-9
MAX_ORDER, input(), int(), print()
PRICE_MEDIUM

Variable String function Indentation


number, size, total <string>.upper() Lines 17, 19-20, 22

An integer variable A string variable A real variable


number size total

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
8

You might also like