Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This FOR ... TO as range() in Python worksheet is part of the IGCSE Computer Science 0478 — 7c Pseudocode and Flowcharts journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1for i in range(1, n + 1): | AOff-by-one error: range(1, 10) stops at 9, so the loop misses the value 10. | |
| 2for i in range(10, 0, -2): | Bfor num in range(0, 21, 5): | |
| 3The pseudocode FOR Count ← 1 TO 5 runs for the values 1, 2, 3, 4 and 5 | CPython version of FOR i ← 1 TO n; the + 1 makes the loop include n | |
| 4The pseudocode FOR Num ← 0 TO 20 STEP 5 becomes range() with a third argument for the STEP | Dfor count in range(1, 6): | |
| 5for i in range(1, 10): # FOR i ← 1 TO 10 | EThe end value 1 is not included when counting down, so the loop stops at 2 and misses 1. | |
| 6for i in range(5, 1, -1): # FOR i ← 5 TO 1 STEP -1 | FCounts down 10, 8, 6, 4, 2 in steps of 2 and stops before reaching 0 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
for i in range(1, n + 1): — Python for FOR i ← 1 TO n. range() stops before its end value, so use n + 1 to include n. A third argument is the STEP; a negative step counts down.# FOR i <- 1 TO n
for i in range(1, n + 1):
print(i)# FOR i <- 1 TO 5
for i in range(1, 6):
print(i)# FOR i <- 0 TO 20 STEP 5
for i in range(0, 21, 5):
print(i)# FOR i <- 5 TO 1 STEP -1
for i in range(5, 0, -1):
print(i)# FOR i <- 10 TO 2 STEP -2
for i in range(10, 1, -2):
print(i)