/en-tutor-prep-ib-cs-programming-fundamentals-variables-substrings-exceptions/
/en-tutor-prep-ib-cs-programming-fundamentals-variables-substrings-exceptions/
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

IB Computer Science: Programming Fundamentals — Tutor Lesson Plan (variables, substrings, exceptions and debugging; 451 of 500 rows usable)

IB Computer Science: Programming Fundamentals — Tutor Lesson Plan (variables, substrings, exceptions and debugging; 451 of 500 rows usable)

  • 2026-11-28

Students can print a line but cannot explain the difference between 7 and “7”, and one odd receipt stops the whole program. Programming fundamentals is not syntax recall: it is data types, scope and failure handling.

Objectives — the student can (1) give one literal for each of the five data types (True is Boolean, “A” is a char, 12.5 is decimal, 20 is integer and 2026-4A-0731 is a string) and explain why “7” plus 1 fails while int(“7”) plus 1 gives 8; (2) trace global and local variables: three top-ups of 20, 35 and 50 give 105, while a function missing global leaves 0; (3) slice a 12-character card number into year, class and sequence, build an email and replace the class; (4) read 500 rows with three exception branches: 37 badly formatted (7.4%), 12 negative (2.4%) and 451 usable (90.2%), with finally running 3 times; (5) trace four top-ups as 20, 55, 105 and 120, then find the range error that sums only 100, short by 20 (16.7%); (6) state what print, a trace table, a breakpoint and step-by-step execution each show.

01 Prerequisites (3-minute check)

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

Skill Quick check Pass mark
Assignment x starts at 5 and the program runs x = x + 3, what is x? 8
Type conversion what are “7” plus 1 and int(“7”) plus 1? the first raises an error, the second gives 8
Comparison a balance of 12.5 with a top-up of 20, is that enough for a 30 meal? yes, the balance becomes 32.5

02 Lesson flow (60 minutes)

Time Stage Tutor does Student does
0-7 min Opening check what is the difference between 7 and “7”? write an answer first
7-20 min Five types and scope card 2026-4A-0731, six top-ups totalling 245 fill the type table, compute the balance
20-32 min Substrings and string building 12 characters into three parts, then an email write the slices and the replacement
32-45 min Three-part exception handling 500 rows, three kinds of failure write try and the except branches
45-55 min Trace table and four debugging tools four top-ups, a range error draw the trace, find the bug
55-60 min Exit check three questions marked on the spot work alone

03 Teaching script (read it out)

Scene 1: Five data types and scope: start with 7 against “7”

Open with a canteen top-up card: the card number 2026-4A-0731, a starting balance of 12.5 and six top-ups of 20, 35, 50, 15, 100 and 25 during the term, 245 in total, leaving 257.5 at the end.
Write the five data types as one table: Boolean holds only True and False, for example whether the card is frozen; char is a single character, “A”, of length 1, written with single quotes in Java, while Python has no separate char and a string of length 1 plays that role; decimal holds a fractional number such as 12.5, a double in Java and a float in Python; integer holds a whole number such as 20; string holds text such as 2026-4A-0731, which is 12 characters. Ask the class which type AB is: string, because its length is 2.
Then the type traps. In binary, 0.1 plus 0.2 is not 0.3 but 0.30000000000000004, because 0.1 cannot be stored exactly. Money therefore goes in cents as integers: 10 cents plus 20 cents is 0.3 exactly. Division is the second trap: 245 divided by 6 gives 40.8, while floor division gives a quotient of 40 with remainder 5. A student who answers 40 has answered the quotient and lost the 5.
Close on scope. A global TOTAL starts at 0; a function add(amount) only changes it if it declares global TOTAL. Three calls of 20, 35 and 50 leave 105; without the global declaration the function writes a fresh local variable and the global stays 0. Ask for one sentence: a local variable lives only until the function returns, so reading it outside raises NameError.

Student task: declare five variables for the card (number, balance, frozen, class, transaction count), label each with a data type, and explain why the balance is kept in cents.

Scene 2: Substrings and exceptions: cut the string, sort the failures

Start with substrings. The card number 2026-4A-0731 has 12 characters and indexing starts at 0: [0:4] gives 2026, [5:7] gives 4A and [8:12] gives 0731; split on a hyphen gives three parts, 2026, 4A and 0731. The commonest slicing error is the right-hand bound: the last character sits at index 11, so [8:11] drops the final digit and the sequence becomes 073.
Build and replace: join the three parts with dots to get 2026.4A.0731@school.edu.hk, then replace 4A with 4B to get 2026.4B.0731@school.edu.hk. Stress that replace returns a new string and does not change the original, so it must be assigned back; that is exactly why a student who writes replace and sees no change is puzzled.
Now exception handling on a 500-row transaction file: 37 rows carry a badly formatted amount (7.4%, for example 12,5 with a comma), 12 rows are negative (2.4%, a top-up cannot be negative) and the remaining 451 rows are usable (90.2%). Catch the three kinds in three except branches rather than one bare except: a format error needs a message to the user, a negative needs rejection, a missing file needs a different message. finally always runs, so 3 calls produce 3 runs even when 2 of them raise: close the file and write the audit line there.
Map it to Java: try, catch and finally correspond one to one, with the exception class named in the catch, such as NumberFormatException or ArithmeticException. Also teach that a logic error raises nothing at all: adding instead of subtracting still runs and only the answer is wrong, so only a trace table and test data catch it.

Student task: write three except branches for the 500-row file and one sentence on which kind of error raises no exception and what catches it.

Scene 3: The trace table and four debugging tools: find the missing 120

Give the four tools a purpose each: print is fastest but must be removed before submission; a trace table suits loops and accumulators; a breakpoint stops the program to show every variable; step-by-step execution walks one line at a time to show which branch was taken. All four are examinable, but the trace table is the one that can be written on paper.
Trace four top-ups of 20, 35, 50 and 15: record i, amount and total each pass, giving totals of 20, 55, 105 and 120, with 120 printed at the end. That table is the evidence of what the program ought to do, and every later edit is checked against it.
Then plant two real bugs. The first is a range boundary: for i in range(1, 4) over a list of 4 items walks 1, 2 and 3, skips index 0 which holds 20, and sums only 100, short by 20 (16.7%). The second is a while boundary: with a list of 4 items, while i is less than or equal to 4 reaches i equal to 4 and reading a[4] raises IndexError; the correct test is i below 4, or a for loop over the list.
Add a Boolean boundary: a card locks after 3 wrong PINs, but if tries is compared with greater than 3 the third wrong PIN still works. Boundary errors between greater than and greater than or equal to are the most common lost marks in Paper 2, so ask the class to say what should happen in the equal case every time they write a condition.

Student task: add a fifth top-up of 40 to the trace, write the new final row, and say which item for i in range(len(a) – 1) would skip.

04 Question types and worked steps

Type Example Steps Answer
Identify a literal type True, “A”, 12.5, 20, 2026-4A-0731, AB three questions: quotes mean string (length 1 makes it a char), a decimal point means decimal, only True and False are Boolean Boolean, char, decimal, integer, string, string
Global and local variables global TOTAL starts at 0, three top-ups of 20, 35, 50 two steps: check whether the function declares global, then decide whether the outer value moves with global 105; without it 0
Extract and build substrings card 2026-4A-0731 into year, class and sequence, then an email three steps: count the length, write left-closed right-open slices, join and assign the replacement back 2026, 4A and 0731; 2026.4A.0731@school.edu.hk; replaced class gives 2026.4B.0731@school.edu.hk
Three-part exception handling 500 rows, 37 malformed, 12 negative three steps: wrap the risky line in try, catch each failure kind separately, put closing work in finally 451 usable (90.2%), three except branches, finally runs 3 times
Trace to find a bug a list of 4 items, the program uses range(1, 4) three steps: record i and the variable each pass, compare with the expected total, name the skipped index and the shortfall total 100, index 0 skipped, short by 20 (16.7%)
Sanity check (teach this): five checks: (1) count string length with len, so 2026-4A-0731 has 12 characters because the two hyphens count; (2) a slice right bound is the last index plus one, so [8:12] returns all four digits of the sequence; (3) money is kept in cents, so 0.3 is 30 cents and the trailing digits of 0.30000000000000004 never appear; (4) the number of except branches must match the number of failure kinds (malformed, negative, missing file), because one bare except cannot say what to report; (5) a running total over positive top-ups must rise every pass, and a dip means the wrong variable was added. The commonest misjudgement is to treat a program that does not crash as correct: replacing plus with minus raises nothing and still finishes, with 100 instead of 120, and only a trace table or test data catches it.

05 Independent practice (3 questions)

Question Answer What to watch
Card 2026-4A-0731. (a) Write the slices for the year and the sequence. (b) What is the email after the class is changed from 4A to 4B? (a) [0:4] gives 2026 and [8:12] gives 0731; (b) 2026.4B.0731@school.edu.hk the right bound is the last index plus one; replace must be assigned back
A 500-row file has 37 badly formatted rows and 12 negative rows. (a) How many rows are usable and what percentage is that? (b) How many times does finally run across three calls? (a) 451 rows, 90.2%; (b) 3 times, even when an exception is raised the three kinds must add to 500; finally is independent of success
A list holds (20, 35, 50, 15) and the program sums with range(1, 4). Find the total, the missing amount and the correct range. total 100, missing 20 (16.7%); use range(4) or iterate the list directly indexing starts at 0 and the range right bound is excluded

06 Common mistakes and what to say

Mistake Student writes Say this
Treating “7” as 7 writing total = total + “7” and reporting a TypeError quotes make it a string, and a string only joins another string. Convert first: int(“7”) is 7. The card number 2026-4A-0731 is a string and cannot be added or subtracted, which is why the five data types matter.
Assuming division always gives an integer answering that 245 over 6 gives 40.8, so each pays 40 the / operator gives 40.8; // gives quotient 40 with remainder 5 still unshared. On an integer question, state which operator is used, or the answer is off by an order of magnitude.
A bare except that swallows everything writing except: pass pass hides the error: the program runs on with missing data and nobody knows. 37 malformed and 12 negative rows out of 500 need separate branches and a report of 451 accepted and 49 rejected. That is what handling means.
A trace table with only the final answer writing 100 with no i or per-pass values every pass must be recorded or the missed 20 at index 0 never shows up. Without the rows, the 20 shortfall cannot be found.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
Types one literal each for Boolean, char, decimal, integer, string no char and string distinction means restart scene one
Exceptions 451 of 500 usable, finally 3 times a single bare except is not finished
Trace 20, 55, 105, 120 skipping index 0 means trace again

09 Tutor FAQ

Is Paper 2 in Python or Java?

The school chooses one and both are accepted; the concepts correspond. This page uses Python, and the Java differences are only three: char is a separate type, strings are compared with equals, and exceptions use try, catch and finally.

Python has no char, so how is a char question answered?

Python uses a string of length 1 in that role, so len(“A”) is 1, while a Java char holds one UTF-16 unit in single quotes. Answer that a char is a single character and note that AB has length 2 and is therefore a string.

Must all four debugging tools be memorised?

Know what each is for: print is fastest, a trace table survives on paper, a breakpoint shows every variable at one moment, and step-by-step execution shows the branch taken. Exam questions usually give code and ask for the output, where a trace table is safest; real projects use breakpoints.

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 →