Computing Lesson 2 Trace Tables Programming Part 3

Computing Lesson 2: Trace Tables Programming Part 3: Iteration Rebecca Franks 1 Materials from the Teach Computing Curriculum created by the National Centre for Computing Education

Trace table 1 Create your own trace table for the program below using the table that has been started for you. 1 from time import sleep 2 count = 5 3 while count != 0: 4 print(count) 5 count = count - 1 6 sleep(1) Line 2 Variable count 5 3 5 4 3 4 Add more lines as needed 2 Output True 4 5 Condition count!=0

Trace table 2 Create your own trace table for the program below using the table that has been started for you. 1 number = 2 2 while number < 11: 3 print(number) 4 number = number + 2 Line 1 Variable number 2 Condition number < 11 2 3 4 Add more lines as needed 3 Output True 2

Trace table 3 Variable Create your own trace table for the program below using the table that has been started for you. 1 2 3 4 5 6 7 8 9 10 4 a = 21 b = 15 while a != b: if a < b: b = b-a else: a = a-b print(a) Line a 1 21 2 Condition b a != b 15 4 5 8 4 Add more lines as needed True a < b Output

Detect and correct errors The following program contains an error. You must create a trace table and use this to help you detect the error in the program. 5

Trace table 4 Line This while loop has been designed to print the 5 times table, up to 25. Stop completing 1 the trace table once you have spotted the 3 error. 1 number = 1 2 3 while number != 25: 4 number = number + 5 5 print(number) Variable number 1 Condition number != 25 4 5 3 4 Add more lines as needed 6 True Output

Correct the error Using your trace from the previous task as a guide. Rewrite the program for displaying the 5 times table. A. Test your code by creating a trace table. B. Test your code in Repl. it. 7
- Slides: 7