Tutorial 3
CS 110
1
While Loops
2
while some condition is True:
# execute code block
Pseudocode
While Loops
3
Example 1: Infinite Loop
while True:
print('Hello! How are you doing today?')
time.sleep(1)
In this case, the loop will never terminate because the expression (i.e. True) will always be true.
Test it: warmup/a_while_always_true.py
4
Example 2: Loop that terminates after 5 iterations
counter = 0
while counter < 5:
print('Hello! How are you doing today?')
time.sleep(0.5)
counter += 1�
In this case, the loop terminates after 5 iterations
Test it: warmup/b_while_termination_condition.py
5
Example 2: Algorithm
counter = 0
counter += 1�
6
Python keywords that are important for loops
7
Your Turn!
8
Activity 1: Numbers Game
Finish writing the 01_number_game.py program so that it works as follows:
9
Activity 2: Drawing a Vertical Line of Circles
Hints:
10
Optional: When you’re done, try to make these drawings...
11
3
7
6
5
4