Lesson 7.5 · Trace it
Trace: linear search
Python code6 rows to fill4 marks
Complete the trace table for this Python code. The loop stops once found is True, or once no values are left. Then answer the question below the table. [4]
Line 1: values = [6, 2, 9, 4]Line 2: target = 9Line 3: i = 0Line 4: found = FalseLine 5: while i < len(values) and not found:Line 6, indented 1 level: if values[i] == target:Line 7, indented 2 levels: found = TrueLine 8, indented 1 level: else:Line 9, indented 2 levels: i = i + 1Line 10: print(i)
Fill in one row each time a line changes i or found, 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 | i | found | output |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 |