1
Vocabulary β Define each term
Concept
2
For Loop vs. While Loop
Compare
Complete the comparison table below.
| Aspect |
π For Loop |
β While Loop |
| Repetitions |
Fixed β known in advance |
|
| Controlled by |
|
A condition (True/False) |
| Use when⦠|
|
|
| Main risk |
Off-by-one errors |
|
| Karel example |
for i in range(5): move() |
|
3
While Loop Anatomy
Syntax
Label each numbered part of the while loop below. Choose from the word bank.
Word Bank
keyword Β· condition Β· colon Β· loop body Β· indentation
1
while front_is_clear():
- On line 1, the word
while is called the _____________________ of the loop.
- On line 1,
front_is_clear() is the _____________________ β it evaluates to True or False.
- On line 1, the
: at the end is required, just like in _____________________ definitions.
- On line 2,
move() is inside the _____________________. It runs each time the loop repeats.
- On line 2, the 4 spaces before
move() are called _____________________. Without them, Python shows an error.
4
Fill in the Blanks
Apply
Complete each while loop by filling in the missing parts on the green lines.
4A. Move Karel to the wall on the right.
4B. Turn Karel to face north (it could be facing any direction).
4C. Karel should pick up all balls on a single row. Fill in both blanks.
Karel starts at column 1, facing east, in a world that is 6 columns wide. Trace through the code and complete the table.
1
while front_is_clear():
4
put_ball() # after the loop
| Iteration # |
front_is_clear()? |
Karel's Column |
Ball placed at col |
| Start |
β |
1 |
β |
| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| After loop (line 4) | False β exit | | |
- How many total balls does Karel place? ________
- Why is
put_ball() on line 4, outside the loop? This is called a _________________ bug fix.
- Would a
for loop work as well here if the world size changed? Explain:
6
Spot the Bug π
Debug
Each code snippet below has a problem. Identify the bug and write the corrected code.
6A. This loop never runs at all:
1
while front_is_clear():
2
move() # no indentation!
What is wrong?
Write the fix:
1
_________________________________
2
_________________________________
6B. This loop causes an infinite loop. Why?
1
while not facing_north():
What is wrong?
What command should replace move() on line 2? ______________________
6C. Karel should place a ball on every square. What is missing?
1
while front_is_clear():
What is missing? (Hint: think about the last square.)
Write the complete fixed code on the correct numbered lines:
1
_________________________________
2
_________________________________
3
_________________________________
4
_________________________________
7
Write Your Own Code
Create
For each problem, write a while loop solution on the numbered lines. Be sure to indent properly.
7A β Move to Wall (1.12.3)
Karel starts somewhere facing east. Write a while loop that moves Karel forward until it hits a wall. Your solution should work on any world size.
1
_________________________________
2
_________________________________
7B β Lay Row of Tennis Balls (1.12.5)
Karel should place a ball on every square in a row until it hits a wall β including the last square. Use a while loop with a fencepost fix.
Fencepost Reminder
The loop stops when front_is_clear() is False β but Karel still needs to place a ball on that last square!
1
_________________________________
2
_________________________________
3
_________________________________
4
_________________________________
7C β Big Tower Challenge π (1.12.6)
Challenge
Karel starts at the bottom of a tower, facing north. Write code to: (1) turn Karel to face north using a while loop, then (2) build upward by placing a ball and moving up while the front is clear.
1
# Step 1: Turn to face north
2
_________________________________
3
_________________________________
4
# Step 2: Build the tower
5
_________________________________
6
_________________________________
7
_________________________________
8
_________________________________
8
Short Answer / Reflection
Reflect
-
When would you choose a while loop instead of a for loop? Give a real-world or Karel example.
-
What is the most important question to ask yourself before writing a while loop? Why?
-
In your own words, what makes a Karel solution "general"? Why does this matter?
-
A classmate says: "I'll just use a for loop with a really high number like 1000 so it always runs enough times." What is wrong with this idea?
9
Kahoot! Unit 1 Review
Game Review
Circle the letter of the correct answer for each question, then write why on the answer line below.
What does this code do?
1
while front_is_clear():
β²
A. Moves Karel exactly 5 times
β
B. Moves Karel until it hits a wall
β
C. Turns Karel left forever
My answer: _____ Why?
Which loop should you use when you don't know how many times to repeat an action?
My answer: _____ Why?
What is an "infinite loop"?
β²
A. A loop that runs exactly once
β
B. A loop whose condition never becomes False
β
C. A loop inside a function
β
D. A loop with no body
My answer: _____ Why?
What does top-down design mean in Karel programming?
β²
A. Writing code from bottom to top
β
B. Breaking a big problem into smaller functions first
β
C. Using while loops before for loops
β
D. Commenting every single line
My answer: _____ Why?
How does a for loop differ from a while loop?
β²
A. For loops run forever; while loops don't
β
B. For loops repeat a fixed number of times; while loops repeat based on a condition
β
C. For loops can't use functions
β
D. While loops are faster
My answer: _____ Why?
Karel places balls on a street until hitting a wall. One extra ball is needed at the very end. This is called aβ¦
β
D. Abstraction mistake
My answer: _____ Why?
π Exit Ticket
On the lines below, write the
ONE thing you now understand about while loops that you didn't before today's lesson.