Skip to content
ExamSpine

Lesson 5.4 · Trace it

Trace: count in a word

Python code8 rows to fill3 marks

Complete the trace table for this Python code. word[i] is the character at index i, counting from 0, and len(word) is the number of characters. [3]

Line 1: word = "noon"
Line 2: first = word[0]
Line 3: count = 0
Line 4: for i in range(len(word)):
Line 5, indented 1 level: if word[i] == first:
Line 6, indented 2 levels: count = count + 1
Line 7: print(count)

Fill in one row each time a line changes i or count, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowicountoutput
1
2
3
4
5
6
7
8