Lab 6: Knowledge Check
These questions check your knowledge of concepts from this lab. Use the button at the bottom to show your answers once you've attempted them.
-
Which of the following are useful techniques for debugging issues with a loop? (pick one or more)
- Add a print statement on the first line of the loop to see how many times it iterates.
- Add a print statement within the loop to print the values of key variables or expressions.
- Use the Thonny debugger to step through the loop, using breakpoints to fast-forward to each iteration.
-
What should you do if code containing a loop keeps running for a long time? (pick one or more)
- Let it run for at least 15 minutes. It will eventually stop.
- Stop it, using control-C, or the stop button in Thonny/Jupyter.
- Add print statements so that you can see more about what it is doing.
- Change any for loops into while loops to make sure they will stop.
- Make sure to clear the output in Jupyter so that the notebook doesn't have loading issues the next time you open it.
-
If we run the following code, what is the sequence of numbers that gets printed (ignoring formatting)?
x = 0 while x < 7: x += 2 print(x) x += 2 print(x) x += 2 print(x)
- 2, 4, 6
- 2, 4, 6, 8
- 2, 4, 8
- 2, 4, 8, 10
- 2, 4, 8, 10, 12
- 2, 4, 8, 10, 14, 16, 18
-
If we run the following code, what gets printed?
s = 'hello' r = '' for i in range(len(s)): r = s[i] + r print(r)
- olleh
- lleh
- hello
- hell
Table of Contents
- Lab 6 Home
- Part 2: Loops with Strings (
for
loops) - Part 3: HiLo Game (
while
loops) - Part 4: Loops and Graphics
- Knowledge Check