VertexMath · MTH1W · Free worksheet
Coding with Python Review
Try each problem first. You don’t need to finish everything.
K = Knowledge & Understanding · A = Application · T = Thinking · Boss = Challenge
Show your work on separate paper. Show your work in the dot-grid space.
Core Skills (K)
Variables · Operators · Data Types · Loops · Conditionals
K1 Trace the Variables
Determine what each program prints.
- a)
- b)
- c)
Stuck? Peek at a hint
Work line by line: the right side is computed first, then stored in the variable on the left, replacing what was there.
K2 Python Operators
Evaluate each pair of expressions in Python.
- a) and
- b) and
- c) and
Stuck? Peek at a hint
Python does , , and from left to right, after brackets and .
K3 Numbers or Text?
Determine the value and the data type (, or ) of each result.
- a)
- b)
- c) The user types when this program runs.
Stuck? Peek at a hint
Quotation marks make a . Check whether or has turned the text into a number before you calculate.
K4 Loops and range
Determine what each program prints.
- a)
- b)
- c)
Stuck? Peek at a hint
List the values of the loop variable first: starts at and stops before .
K5 Branching
Determine what this program prints for each input.
- a) The user types .
- b) The user types .
- c) The user types .
Stuck? Peek at a hint
Test the conditions from the top. Python runs the first branch whose condition is and skips the rest.
Real World (A)
Math Library · While Loops · Branching in Context
A1 Ladder Check
A hardware store's app uses this program to find how high a ladder reaches up a wall.
- a) A 5 m ladder stands with its base 1.4 m from a wall. Determine the output. How high up the wall does the ladder reach?
- b) A painter always sets the base one quarter of the ladder's length from the wall. Rewrite the line so the program calculates it instead of asking. Then determine the output for a 6 m ladder.
- c) Someone types a ladder length of 3 and a base of 4. What happens when the program runs, and what does it mean for the ladder?
Stuck? Peek at a hint
The ladder, the wall and the ground form a right triangle, and the ladder is the hypotenuse.
A2 Saving for a Bike
Maya has $50 and saves $35 every week for a $480 bike. Her program counts the weeks.
- a) Determine the output. What does it tell Maya?
- b) Maya already has $500 when she starts, so the first line becomes . Determine the output and what it means.
- c) A friend calculates and says Maya needs 12.29 weeks. Who is right, Maya's program or her friend?
Stuck? Peek at a hint
Track and each time the loop runs, and test the condition before every pass.
A3 Electricity Bill
An Ontario utility charges 10 cents per kWh for the first 600 kWh in a month, and 12 cents per kWh for each kWh above 600. This program prints the bill in dollars.
- a) Determine the output for 450 kWh and for 900 kWh. What do the outputs mean?
- b) A family's bill was $120. How much energy did they use?
- c) The meter reader types by mistake. Determine the output. Does it make sense for the bill? Write an condition that would catch this kind of mistake.
Stuck? Peek at a hint
In (b), $120 is an output of the program, so work backwards from the branch that gives bills over $60.
Think It Through (T)
Error Analysis · Building Code from a Graph
T1 Spot the Flaw
A student wants to add :
"My trace table shows that the total ends at 10, so the program prints 10."
- a) Determine what the program actually prints.
- b) Identify the line that causes the problem, explain why, and fix the program.
- c) To add the whole numbers from 1 to 100, the student starts the fixed loop with instead. Determine what it prints, and correct the loop.
Stuck? Peek at a hint
Trace the program one pass at a time, writing down after every line inside the loop.
T2 Build It from the Graph
The graph shows the cost , in dollars, to ship a parcel of mass kilograms, for .
- a) Write a program that reads and prints , using , and .
- b) Determine the cost of a 3 kg parcel and of a 3.2 kg parcel from the graph.
- c) A student's program uses as its first condition and after it. Find the mass for which it disagrees with the graph.
Stuck? Peek at a hint
A closed dot means that endpoint belongs to the piece, which matches ; an open dot matches .
Boss Level
Optional challenge
Count Without Running
- a) Determine the output without running the program.
- b) The is changed to . Determine the new output.
- c) With again, is replaced by a whole number . Find the smallest for which the program prints .
Stuck? Peek at a hint
Count the multiples of 3 and the multiples of 5 separately, then look for the numbers you counted twice.
Answer key
Final answers only. Try first, then check.
Core Skills
- a)
- b)
- c)
- a) and
- b) and
- c) and
- a) , a
- b) , an
- c) ( is the )
- a) , , , (one per line)
- b) , , (one per line)
- c)
- a)
- b)
- c)
Real World
- a) : the ladder reaches 4.8 m up the wall.
- b) ;
- c) Python stops with a : , and cannot take the square root of a negative number. A 3 m ladder cannot reach a wall 4 m away.
- a) : Maya can buy the bike after 13 weeks, with $505 saved ($25 left over).
- b) : the loop never runs, so she can buy the bike today.
- c) The program: Maya adds $35 once a week, so after 12 weeks she has $470, which is not enough. She needs 13 whole weeks.
- a) and : bills of $45.00 and $96.00
- b) 1100 kWh (from )
- c) : the family would be paid $5, which is impossible because energy used cannot be negative. e.g. then print a warning instead of a bill.
Think It Through
- a)
- b) runs on every pass, so the running total restarts each time. Move above the line.
- c) (it stops at 99); use to get
- a) e.g.
- b) $12 and $20
- c) : the graph gives $8, the student's program gives $12
Boss Level
- a)
- b)
- c)