Year 9 Computing — Blocks and indentation
Read a program's indentation to say which lines belong to which block, and how often each line runs.
Name: ________________________
1.Follow this code by hand. How many times does the most deeply indented line run?
for n in range(1, 5): print(n) if n > 100: print(n * 2)2.The last line has been brought back out to the left. What is total at the end?
total = 0 for n in range(1, 4): total = total + 10 total = total + 13.This program prints four numbers. Put them in the order they appear on the screen.
total = 0 for n in range(1, 4): total = total + 5 print(total) print(total - 12)- 15
- 5
- 10
- 3
4.Sort each line of this program by how many times it runs.
total = 0 for n in range(1, 4): total = total + n print(n) print(total) print(total * 2)Groups: Runs once · Runs three times
- total = total + n
- print(total)
- total = 0
- print(n)
- print(total * 2)
5.A test sits inside the loop here. What is total at the end?
total = 0 for n in range(1, 6): if n > 3: total = total + n6.Both of the printing lines are indented here. How many numbers does it print?
for n in range(1, 4): print(n) print(99)7.Follow this code by hand. What is count at the end?
count = 0 for n in range(1, 7): if n > 2: count = count + 1 count = count + 108.One line is indented here and one is not. How many numbers does it print?
for n in range(1, 4): print(n) print(99)
Answer key — Year 9 Computing — Blocks and indentation
- 1.
- 0
- none
- 2. 31
- 3. 1. 5 2. 10 3. 15 4. 3
- 4. total = 0 → Runs once; total = total + n → Runs three times; print(n) → Runs three times; print(total) → Runs once; print(total * 2) → Runs once
- 5. 9
- 6. 6
- 7. 64
- 8. 4