Constants

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Constants

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line follows the Python convention for naming a constant?
A GST_RATE = 0.1
B gstRate = 0.1
2. Where should constants normally be defined in a program?
A At the top of the program, before they are used
B Buried in the middle of a loop
3. Which statement about a constant is correct?
A Its value is set once and not changed while the program runs
B Its value changes every time the user enters data
4. Which one is best stored as a constant?
A score = 0
B MAX_ATTEMPTS = 3
5. Which line avoids a 'magic number'?
A total = price * 0.1
B total = price * GST_RATE

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1GST_RATE = 0.1 ADefines a constant named GST_RATE with the value 0.1, set once and not changed while the program runs
2total = price * GST_RATE BThe constant is written in lower case. By convention constants use UPPER_CASE, such as GST_RATE, so readers know it must not change.
3A magic number is an unexplained value typed directly into code, which a named constant avoids CThe constant is reassigned partway through the program. A constant must be set once and never changed while the program runs.
4A variable can change as the program runs, but a constant should stay the same DUses the constant's name instead of a magic number, so the 0.1 is not hard-coded in the calculation
5gst_rate = 0.1 Escore = score + 1
6GST_RATE = 0.1 GST_RATE = 0.15 Ftax = price * 0.1

Definitions

GST_RATE = 0.1

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
________ = 0.1
2.
GST _ 0.1
3.
total = price * (1 + ________)
4.
if score >= _________:
5.
seconds = minutes _ SECONDS_PER_MINUTE

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A GST_RATE = 0.1
B price = 50
C gst = price * GST_RATE
D total = price + GST_RATE
E print(total)
Question 2
A MAX_ITEMS = 5
B items = 0
C items = items + 1
D if items > max_items:
E print("Full")
Question 3
A PI = 3.14159
B radius = 4
C area = 3.14159 * radius ** 2
D print(area)
Question 4
A TAX_RATE = 0.1
B price = "200"
C tax = price * TAX_RATE
D print(tax)
Question 5
A MAX SPEED = 100
B speed = 80
C if speed > MAX_SPEED:
D print("Too fast")

Answer Key

Constants
Section A: Quick Questions
Q1
A — GST_RATE = 0.1
Q2
A — At the top of the program, before they are used
Q3
A — Its value is set once and not changed while the program runs
Q4
B — MAX_ATTEMPTS = 3
Q5
B — total = price * GST_RATE
Section B: Match the Pairs
Q1
GST_RATE = 0.1 → Defines a constant named GST_RATE with the value 0.1, set once and not changed while the program runs
Q2
total = price * GST_RATE → Uses the constant's name instead of a magic number, so the 0.1 is not hard-coded in the calculation
Q3
A magic number is an unexplained value typed directly into code, which a named constant avoids → tax = price * 0.1
Q4
A variable can change as the program runs, but a constant should stay the same → score = score + 1
Q5
gst_rate = 0.1 → The constant is written in lower case. By convention constants use UPPER_CASE, such as GST_RATE, so readers know it must not change.
Q6
GST_RATE = 0.1 GST_RATE = 0.15 → The constant is reassigned partway through the program. A constant must be set once and never changed while the program runs.
Section C: Definitions
Q1
GST_RATE = 0.1 — A constant: a named value set once and never changed while the program runs. Written in UPPER_CASE by convention, it replaces magic numbers and is easy to update in one place.
Section D: Fill in the Blanks
Q1
GST_RATE = 0.1
Q2
GST = 0.1
Q3
total = price * (1 + GST_RATE)
Q4
if score >= PASS_MARK:
Q5
seconds = minutes * SECONDS_PER_MINUTE
Section E: Spot the Error
Q1
D — This adds the rate (0.1) to the price instead of the GST amount. It should be total = price + gst.
Q2
D — The constant was defined as MAX_ITEMS in upper case. Python is case-sensitive, so max_items causes a NameError. It should be MAX_ITEMS.
Q3
C — This types the number directly (a magic number) instead of using the constant PI. It should be area = PI * radius ** 2.
Q4
B — The quotes make price a string, so multiplying it by the float TAX_RATE causes a TypeError. It should be price = 200.
Q5
A — Names cannot contain spaces, so this is a syntax error. Use an underscore: MAX_SPEED = 100.