Lesson 4 Repeated Practice Makes Perfect Chapter 1: Introduction Chapter 2: Logical operators Chapter 3: While loops [3.1] While loops [3.2] Practice questions [3.3] Triangle of stars [3.4] Wider triangle of stars [3.5] Infinite loops [3.6] While you code [3.7] More on infinite loops Chapter 4: Applied problem solving Chapter 5: Breaking the flow Chapter 6: More applied problems Chapter 7: Enhancing your robot Chapter 8: Debugging your code Chapter 9: The PEP 8 style guide Chapter 10: Summary Chapter 3: While loops Practice questions face Josiah Wang Here are a few questions for you to try to test your understanding of while loops. 1 2 3 4 5 6 ❮ ❯ Question 1 What is output of the following code snippet? Write multiple outputs on separate lines. Write Infinite if you think the code with run forever without stopping. timer = 1 while timer <= 5: print(timer) Output Infinite Explanation: I started with a trick question! 😈 Since timer is always 1, the program gets stuck in an infinite loop that never ends! Try this yourself (press CTRL+C to stop the program). Check answer! Question 2 What is output of the following code snippet? Write multiple outputs on separate lines. Write Infinite if you think the code with run forever without stopping. timer = 1 while timer <= 5: print(timer) timer = timer + 1 Output 1 2 3 4 5 Explanation: This is the proper version. The program will print 1 to 5 and exit the loop. Always remember to update any counters you have inside your loop! Check answer! Question 3 What is output of the following code snippet? Write multiple outputs on separate lines. Write Infinite if you think the code with run forever without stopping. timer = 1 while timer <= 5: print(timer) timer = timer - 1 Output Infinite Explanation: Here is another case of an infinite loop. This time, because you are always decreasing timer starting from 1, you will never get out of the loop since timer will always be <= 5. Forever. So be careful of such mistakes! Check answer! Question 4 What is output of the following code snippet? Write multiple outputs on separate lines. Write Infinite if you think the code with run forever without stopping. timer = 0 while timer < 6: print(timer) timer = timer + 2 Output 0 2 4 Explanation: Starting with timer == 0, you will increment timer by 2 every time you are in the loop. After the final iteration, timer is 6, which no longer fulfils the while condition timer < 6, so the program exits the loop. Check answer! Question 5 What is output of the following code snippet? Write multiple outputs on separate lines. Write Infinite if you think the code with run forever without stopping. timer = 0 while timer < 6: print(timer) timer = timer + 2 print(timer) Output 0 2 4 6 Explanation: This is the same as the previous verison, except that the program also prints out the value of timer after exiting the loop. This will be 6, since that is the value of timer after exiting the loop. Check answer! Question 6 What is output of the following code snippet? Write multiple outputs on separate lines. Write Infinite if you think the code with run forever without stopping. timer = 0 while timer < 6: timer = timer + 2 print(timer) print(timer) Output 2 4 6 6 Explanation: This time, the program only prints out the value of timer AFTER incrementing its value by 2. So it will print out 6 before exiting the loop, and then print it again outside the loop. Check answer!