/en-tutor-prep-s3-ict-python-loops-functions/
/en-tutor-prep-s3-ict-python-loops-functions/
英文版
3
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

S3 ICT | Python Loops and Functions: Tutor Preparation Guide | for, while, range, accumulator, return

S3 ICT | Python Loops and Functions: Tutor Preparation Guide | for, while, range, accumulator, return

  • 2026-11-12

Students can write for i in range(5), but ask them to add one to ten and they write print(1+2+3). The lesson is not about syntax; it is about one variable remembering a running total.

Objectives — Students can: (1) total 1 to 10 as 55 with for and range(1, 11), explaining that the upper bound is excluded; (2) write the three accumulator steps (set total to 0, add inside the loop, print after the loop) and say why the reset must come first; (3) define a function with a parameter and a return value, such as an average over a list, and separate parameter, local variable and return value.

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Variables After x = 5, what is x if you then write x = 7? 7, because the later assignment wins
Indentation How does Python mark lines that belong to a loop? By indentation, usually four spaces
Input and output How do print and input differ? print writes to the screen; input reads a string from the keyboard

02 Lesson flow (45 minutes)

Time Stage Tutor does Student does
0-6 min Hand first Ask for the sum 1 to 10 by hand, write 55, then demand one program that does it They reach 55 and see a loop is needed
6-16 min for and range Print range(1, 11) then range(1, 12) and compare They state the upper bound is excluded
16-26 min Three accumulator steps Write total = 0, total = total + i, print(total) on three separate lines A four-line program runs and prints 55
26-34 min while and its stop test Redo the task with while, deliberately omitting i = i + 1 They point at the line that stops the loop
34-40 min Functions and return Wrap the total in def add_to(n) and contrast return with print One function with a return value written
40-45 min Exit check Collect three answers and mark on the spot Hand in three answers

03 Teaching script (read it out)

Scene 1: Act 1: the upper bound of range is excluded

“for i in range(1, 11) prints 1 to 10, not 11. Why? Because range means start at the first number and stop before the second. To reach ten, write eleven; to repeat five times, range(5), which is the same as range(0, 5). The third argument is the step: range(2, 11, 2) gives 2, 4, 6, 8, 10, totalling 30. Write range(1, 10) and the total is 45, not 55. That error is the dangerous kind: the program still runs and the answer is quietly wrong.”

Stop before the bound; to include n, write n+1.

Scene 2: Act 2: reset before the loop, print after it

“Four lines: total equals 0; for i in range(1, 11); inside the loop, total equals total plus i; after the loop, print(total). Why reset outside? Put total equals 0 inside and every pass clears it, so the program prints 10. Why print outside? Inside it prints ten running totals: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55. That sequence is useful, but a question asking for the sum wants only the last figure. This variable is an accumulator; its cousins are a counter adding one and a product multiplying by i.”

Reset outside, print after; a misplaced line is silently wrong.

Scene 3: Act 3: parameters are the entrance, return is the exit

“def average(nums), colon, then indented: total equals 0, for n in nums adding, and finally return total divided by len(nums). Three terms to separate. nums is the parameter, filled at the call. total is a local variable that disappears when the function ends. return hands the result back, so the caller needs ans equals average of the list. The usual confusion is print against return: print only displays and the value never leaves; return leaves but displays nothing. Always print the result outside, or you will believe the function did nothing.”

Parameters in, return out; print is not return.

04 Question types and worked steps

Type Example Steps Answer
Total with a loop Write a program that totals 1 to 10. (1) Set total to 0 (2) for i in range(1, 11) (3) add inside the loop (4) print after it total = 0; for i in range(1, 11): total = total + i; print(total) gives 55
while and the stop test Redo the same total with while and name the stopping condition. (1) Set i to 1 and total to 0 (2) while i <= 10 (3) add and increment i (4) print The stop test is i <= 10; without i = i + 1 the loop never ends
Accumulator placement This prints only 10: for i in range(1, 11): total = 0; total = total + i. Why? (1) Find where total is set (2) check whether it is indented (3) move it out (4) run again The reset sits inside the loop and clears the total each pass; outside, it prints 55
Functions and return Write a function returning the sum of even numbers from 2 to 10. (1) Define it with a parameter (2) set total to 0 (3) loop range(2, 11, 2) adding (4) return total and print outside def even_sum(): total = 0; for n in range(2, 11, 2): total += n; return total gives 30
Sanity check (teach this): Sanity check: the total from 1 to 10 must be 55, and 45 means the range is one short; the sum of squares 1 to 5 is also 55, so check the two with different arithmetic rather than using one as proof of the other; the even numbers 2 to 10 are five values totalling 30, so 25 means the ten was dropped; five factorial is 120, which requires product starting at 1, since starting at 0 always gives 0; a nested 3 by 4 loop runs twelve times, not seven.

05 Independent practice (3 questions)

Question Answer What to watch
Print the odd numbers from 1 to 20 with one for loop. for i in range(1, 21, 2): print(i) Start at 1 with a step of 2
Write a function returning the average of 62, 78 and 85. def average(a, b, c): return (a + b + c) / 3, which gives 75.0 Return, then print outside
Why must a while loop be handled carefully? Give one failure. If the stop test never becomes false, the loop runs forever, for example when i = i + 1 is missing Write the stop test first

06 Common mistakes and what to say

Mistake Student writes Say this
A range one short “range(1, 10) covers 1 to 10.” “range stops before its upper bound, so that is 1 to 9 and the total is 45. Write 11 to get 55.”
The accumulator reset inside the loop “Putting total equals 0 in the loop is tidier.” “Each pass clears it, so only the last value survives and you print 10. Reset before the loop and print after.”
Treating print as return “My function prints, so the value is available outside.” “print only displays; the call still returns None. Use return, then print the result at the call site.”
Inconsistent indentation “Indentation is just formatting.” “Python uses indentation to decide which lines belong to the loop, and mixing tabs with spaces raises IndentationError. Four spaces per level.”

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
Does range(1, 11) include 11? No, it covers 1 to 10 Wrong means reteach Act 1
Why reset the accumulator before the loop? Resetting inside clears it each pass, leaving only the last value Wrong means reteach Act 2
What separates return from print? return hands the value back to the caller; print only displays it Confusion means Act 3 again

09 Tutor FAQ

Should list comprehensions be taught?

Not in S3. Get the four-line loop right first; the shorthand belongs to senior years.

Is while True with break acceptable?

It runs, but discourage it. Marks come from a clear stop test, which while True tends to hide.

Is exception handling needed?

Not here. Bad input belongs to the file and data handling chapter; secure loops and functions first.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →