C. Algebra · Algebra: Expressions, Equations and CodingReading, Predicting and Altering Code
C2.3 C2.2 A1About 70 minutes
Learning goals- I can trace code with a trace table and predict its output.
- I can alter a parameter in code to represent a similar situation with different numbers.
- I can alter a constraint, such as a loop range or a condition, and explain how the outcome changes.
- I can adapt code from one situation to a new one, including problems from other strands.
Words to know
trace table
A table with a column for each variable and a row for each step, used to follow code by hand.
parameter (in a model)
A number in the code that describes the situation, such as the amount of fencing or the weekly savings. Changing it gives a similar situation.
constraint
A limit or condition the code must respect, such as a loop range, or the condition in an if or while statement.
outcome
What the code produces: its output or the decision it makes.
debug
Find and fix mistakes in code.
Let's start with a fence
A family has 40 m of fencing for a rectangular garden. They want the largest possible area. Should the garden be long and thin, or closer to a square?
If the width is w, then two widths and two lengths use 40 m, so each length is 20 − w. The area is w(20 − w).
Three gardens with the same 40 m perimeter. The 10 m by 10 m garden has the largest area of these three. Is it the largest of all?Instead of drawing every rectangle, here is code that tries every whole-number width and keeps the best one. Before reading the output, predict it.
best_area = 0
best_w = 0
for w in range(1, 20):
area = w * (20 - w)
if area > best_area:
best_area = area
best_w = w
print(best_w, best_area)
Output: 10 100
Reading code: trace tables
To predict what code does, trace it. Make a column for each variable and a row each time something changes. Here is part of a trace for the fence code:
| w | area | area > best_area? | best_w | best_area |
|---|
| 1 | 19 | yes | 1 | 19 |
| 2 | 36 | yes | 2 | 36 |
| 3 | 51 | yes | 3 | 51 |
| ... | ... | ... | ... | ... |
| 10 | 100 | yes | 10 | 100 |
| 11 | 99 | no | 10 | 100 |
A trace table for the fence code. Each row is one run of the loop.After w = 10, the areas get smaller (99, 96, 91, ...), so best_w and best_area never change again. The output is 10 100: a 10 m by 10 m square.
Graphing the loop's results: area against width. The points rise, peak at (10, 100), then fall. The pattern is not linear, which is why a loop that tries every width is handy.Altering code
Once code works, you can alter it to model other situations:
- Change a parameter: a different amount of fencing, a different price, a different savings goal.
- Change a constraint: a different loop range, or a different condition in an if or while statement.
- Change the model: a new expression for a new situation.
After every change, predict the new output first, then run or trace to check.
Worked exampleExample 1: Change a parameter
Alter the fence code for 60 m of fencing. Predict the output.
- With 60 m, each length is 30 − w. Change the area line to
area = w * (30 - w). - Widths can now go up to 29, so change the loop to
range(1, 30). - Prediction: a square again, 15 m by 15 m, area 225 m².
Worked exampleExample 2: Change the model
The garden is now against a barn wall, so the fence covers only three sides: two widths and one length. There is still 40 m of fence. Alter the code.
- Two widths use 2w, so the length is 40 − 2w.
- Change the area line to
area = w * (40 - 2 * w). The length must stay positive, so w goes up to 19: range(1, 20) still works. - Trace a few: w = 5 → 5 × 30 = 150; w = 10 → 10 × 20 = 200; w = 11 → 11 × 18 = 198.
Output: 10 200. The best barn-side garden is 10 m wide and 20 m long: not a square this time!
Worked exampleExample 3: Change a constraint
In the savings code from the last lesson (start $120, add $35 a week, while savings < 400:), what happens if the condition is changed to while savings <= 400:?
- After 8 weeks, savings = 400. With <, the condition 400 < 400 is False, so the loop stops.
- With <=, the condition 400 <= 400 is True, so the loop runs once more: savings = 435, weeks = 9.
- The output changes from 8 400 to 9 435.
One small symbol changed the outcome. When you alter a constraint, always check the boundary case.
Altering code like a pro
- Before running altered code, write your prediction. Then compare.
- Change one thing at a time, so you know what caused each change in the output.
- Look for every place a parameter appears. In the fence code, “20” appears in the area line, and the loop range depends on it too.
- Code from one strand often fits another: the same loop-and-keep-the-best pattern works for geometry, money and data problems.
Hands-on activityString rectangles
You need: a piece of string exactly 40 cm long (cut with scissors carefully), grid paper or a ruler, a pencil.
- Tie or tape the string into a loop. Its perimeter is 40 cm.
- Shape it into rectangles on the grid paper with widths 2, 5, 8, 10, 12 and 15 cm. Record each width, length and area in a table.
- Which rectangle has the biggest area? Does it match the code's output for 40 m of fencing (10 100)?
- Now pretend one side is a wall (only three sides need string). Use the full 40 cm for three sides and try widths 5, 8, 10 and 12. Which is biggest? Compare with Example 2.
- Write which parameter or line of the code you would change to model a 30 cm string.
Model answer: for the full loop, 10 × 10 = 100 cm² is biggest. Against a wall: w = 10 gives 10 × 20 = 200 cm², bigger than w = 8 (8 × 24 = 192) or w = 12 (12 × 16 = 192). For 30 cm, change the area line to area = w * (15 - w) and the loop to range(1, 15).
Just for funThere are 10 kinds of people in the world: those who understand binary, and those who don't.
(In binary, 10 means two.)
Your turn
1Trace and predict:
x = 2
for i in range(3): x = x * 3
print(x)
x: 2 → 6 → 18 → 54. Output: 54.
2Alter the fence code for 32 m of fencing. Which two lines change, and what is the new output?
area = w * (16 - w) and for w in range(1, 16):. Output: 8 64 (an 8 m by 8 m square).
3The toothpick-table code is for n in range(1, 6): print(n, 3 * n + 1). Alter it to print the triangles pattern 2n + 1 instead. What is printed?
Change the expression to 2 * n + 1. It prints 1 3, 2 5, 3 7, 4 9, 5 11 (one pair per line).
4Predict the output:
count = 0
for n in range(1, 11): with this line indented inside: if 2 * n + 1 > 12: count = count + 1
print(count)
2n + 1 > 12 when n ≥ 6 (n = 6 gives 13). So n = 6, 7, 8, 9, 10 count. Output: 5.
5In the savings code, the goal 400 is changed to 500. Predict the output.
120 + 35w ≥ 500 → w ≥ 10.86, so 11 weeks. Savings: 120 + 35(11) = 505. Output: 11 505.
6In the barn code, which part is the model and which is the constraint?
The model is the area expression w * (40 - 2 * w). The constraint is the loop range(1, 20), which keeps the width between 1 and 19 m so the length stays positive.
7How could you alter the snow-company code (two functions and an if) to compare three companies?
Add a third function, such as def company_c(n): return 100 + 30 * n, then compare all three costs (for example, with if and elif tests, or by finding the smallest of the three with min). Any correct approach that checks all three costs is fine.
More practice: warm-up
1How many times does for n in range(1, 6): repeat?
5 times (n = 1, 2, 3, 4, 5).
2The code says print(cost(3)). How do you alter it to find the cost for 4 hours?
Change it to print(cost(4)).
3Predict:
total = 0
for k in range(1, 4): total = total + k * k
print(total)
4In the fence code, what does the number 20 represent?
Half the fencing: the length plus the width (40 m ÷ 2 = 20 m).
5Predict print(10 * (20 - 10)).
More practice: on your own
1Predict:
x = 100
while x > 10: x = x / 2
print(x)
x: 100 → 50.0 → 25.0 → 12.5 → 6.25. At 6.25, x > 10 is False. Output: 6.25.
2The inequality code for x in range(-5, 6): with if 2 * x + 1 > 5: print(x) is altered to test 3x − 2 ≤ 7. Write the new condition and predict the output.
if 3 * x - 2 <= 7: print(x). 3x ≤ 9 → x ≤ 3. Output: −5, −4, −3, −2, −1, 0, 1, 2, 3.
3Predict: for n in range(1, 4): print(n, n ** 2 - 1)
1 0, 2 3, 3 8 (one pair per line).
4Someone wrote the savings loop with while savings <= 400: and got 9 weeks. Explain the problem and fix it.
With <=, the loop runs one extra time when savings is exactly 400, giving 435 after 9 weeks. The goal was reached after 8 weeks. Fix: use while savings < 400:.
5Write a function best_rect(P) that uses the fence loop for any perimeter P (an even number). What does best_rect(36) give?
def best_rect(P): with indented lines: half = P // 2 (// divides and drops any remainder), best_area = 0, best_w = 0, then the loop for w in range(1, half): with area = w * (half - w) and the same if, then return best_w, best_area. best_rect(36) returns (9, 81).
Challenge
1Look at the outputs of best_rect for P = 32, 36, 40 and 60: (8, 64), (9, 81), (10, 100), (15, 225). What general rule do you notice? Explain why it makes sense.
The best rectangle is always a square with side P/4, and the largest area is (P/4)². A square is the most “balanced” rectangle: making one side longer by some amount makes the other side shorter by the same amount, and the area drops (for example, 11 × 9 = 99 < 100).
2Alter the compound-interest loop (amount multiplied by 1.04 each year) to find how many years it takes $1000 to double at 6% per year. Predict the output.
Change the start to 1000, the multiplier to 1.06 and the condition to while amount < 2000. 1.06¹¹ ≈ 1.898 and 1.06¹² ≈ 2.012, so the output is 12 years.
3Predict the output and describe the pattern: for n in range(1, 6): print(n * (n + 1) / 2)
1.0, 3.0, 6.0, 10.0, 15.0. These are the triangular numbers: the numbers of dots in triangles with 1, 2, 3, 4 and 5 rows. Each one adds the next whole number.
Check your understandingQuiz
1. What is a trace table used for?
2. Predict: x = 1, then for i in range(4): x = x + 2, then print(x).
3. To make the fence code work for 60 m of fencing, you should change:
4. Changing a parameter in a program usually lets you:
5. What does for n in range(2, 5): print(n * 10) print?
6. In the fence code, why is there an if area > best_area: line?
For parents and teachersBig idea: C2.3 asks students to read code to predict its outcome, and to alter code to adjust constraints, parameters and outcomes to represent a similar or new mathematical situation. This lesson uses the same short programs in several situations (fencing, savings, interest), connecting algebra coding to geometry and financial literacy, as the curriculum intends (C2 applies coding across the strands).
Common difficulties:
- Running code without predicting first. The prediction is where the thinking happens.
- Changing a parameter in one place but not another (the 20 in the fence code also controls the loop range).
- Boundary errors: < versus <=.
How to help: Do the string-rectangle activity and compare with the code's output. Ask: “If we had 50 m of fence, what would you change in the code?” and have your teen point to the exact lines.