What you will learn
- Define and identify: while loop
- Use a while loop to simulate game play
Reading
Do Now
1. In your Notebook
Answer the following
- How would you print out something 10 times? What about 100? What about 1,000?
- Can you remember something from Snap! that might allow that?
2. Open up the console
Type the following code using interactive mode
while True:
print('a')
3. In your Notebook
Write responses for the following
- What happens when you run this code?
-
Try using other Boolean expressions instead of
True(e.g.4 > 5and9 != 2), and explore what happens. -
How would you print out something 10 times? What about 100? What about 1,000?
- Can you remember something from Snap! that might allow that?
4. Open up the console
Type the following code
while True:
print('a')
5. In your Notebook
continue responses for the following
- What happens when you run this code?
- Try using other Boolean expressions instead of
True(e.g.4 > 5and9 != 2), and explore what happens.
6. Open up the console
Type and run the following code (Remember this from Unit 2?)
if animal in ['cat', 'dog']:
print("A great pet!")
Lab
1. In your Notebook
Predict what will be printed then type the program in your console to confirm
Example 1
a = 0
while a < 10:
print(a)
| Predicted Output | Actual Output |
|---|---|
Example 2
a = 0
while a < 10:
a = a + 1
print(a)
| Predicted Output | Actual Output |
|---|---|
2. In your Notebook
Create a set of test cases for the following sample code and predict the behavior
a = input("Would you like to quit: ")
while a != "y" and a != "n" :
a = input("Would you like to quit: ")
| Predicted Output | Actual Output |
|---|---|
3. Implement the Tic Tac Toe game using a while loop
- Allow users to keep playing (max 9 times).
- Use variables to decide whose turn it is, and greet them as Xs or Os.
- User picks a location on the board according to the number:
- Depending on the position user gave, update the corresponding position of the board to reflect that.
- Print the updated board out.
- You will not need to determine the winner at this point.
Bonus
Create a variable-sized board. So instead of a classic 3 x 3 board, create a way for the user specify the size of the board they want to play with.
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. What does this code print?
n = 0
while n < 3:
n = n + 1
print(n)
- 3
- 0
- 2
- Nothing, it loops forever
The loop repeats while n < 3, and stops once n reaches 3.
2. Why does this loop never end?
count = 0
while count < 5:
print(count)
- Because
printcannot be used in a loop - Because 5 is too large
- Because
countnever changes, so the condition stays true - Because
whileloops run only once
Nothing inside the loop changes count, so count < 5 is always true.
3. When does the code inside a while loop run?
- Only once, before the condition is checked
- Repeatedly, as long as the condition is true
- Only when the condition is false
- Exactly 10 times
A while loop checks its condition before each pass and repeats the body while it is true.