Lesson 3.8 · Trace it
Trace: digit sum
Python code12 rows to fill3 marks
Complete the trace table for this Python code. In Python, // divides and keeps only the whole number part, and % gives the remainder. [3]
Line 1: n = 472Line 2: total = 0Line 3: while n > 0:Line 4, indented 1 level: digit = n % 10Line 5, indented 1 level: total = total + digitLine 6, indented 1 level: n = n // 10Line 7: print(total)
Fill in one row each time a line changes n, digit or total, 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.
| Row | n | digit | total | output |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 | ||||
| 11 | ||||
| 12 |