1.12 While Loop Student Worksheet
 
While Loops in Karel β€” Student Worksheet
CodeHS Β· Unit 1 Β· Lesson 1.12
While Loops
in Karel
Student Practice Worksheet
CS Β· Python with Karel
1.12.1 – 1.12.6
Score:
1
Vocabulary β€” Define each term
while loop
condition
infinite loop
general solution
fencepost bug
iteration

2
For Loop vs. While Loop

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

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():
2
    move()
  1. On line 1, the word while is called the _____________________ of the loop.
  2. On line 1, front_is_clear() is the _____________________ β€” it evaluates to True or False.
  3. On line 1, the : at the end is required, just like in _____________________ definitions.
  4. On line 2, move() is inside the _____________________. It runs each time the loop repeats.
  5. On line 2, the 4 spaces before move() are called _____________________. Without them, Python shows an error.

4
Fill in the Blanks

Complete each while loop by filling in the missing parts on the green lines.

4A. Move Karel to the wall on the right.

1
while :
2
move()

4B. Turn Karel to face north (it could be facing any direction).

1
while not :
2
    

4C. Karel should pick up all balls on a single row. Fill in both blanks.

1
while :
2
    pick_ball()
3
    

5
Code Tracing

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():
2
    put_ball()
3
    move()
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
  1. How many total balls does Karel place? ________
  2. Why is put_ball() on line 4, outside the loop? This is called a _________________ bug fix.
  3. Would a for loop work as well here if the world size changed? Explain:

6
Spot the Bug πŸ›

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():
2
    move()

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():
2
    put_ball()
3
    move()

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

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
  1. When would you choose a while loop instead of a for loop? Give a real-world or Karel example.

  2. What is the most important question to ask yourself before writing a while loop? Why?

  3. In your own words, what makes a Karel solution "general"? Why does this matter?

  4. 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

Circle the letter of the correct answer for each question, then write why on the answer line below.

kahoot!
QUESTION 1 OF 6  Β·  Unit 1 Review  Β·  Units 1.1 – 1.12
What does this code do?
1
while front_is_clear():
2
    move()
β–²
A. Moves Karel exactly 5 times
β—†
B. Moves Karel until it hits a wall
●
C. Turns Karel left forever
β– 
D. Picks up all balls

My answer: _____    Why?

kahoot!
QUESTION 2 OF 6
Which loop should you use when you don't know how many times to repeat an action?
β–²
A. for loop
β—†
B. def function
●
C. if statement
β– 
D. while loop

My answer: _____    Why?

kahoot!
QUESTION 3 OF 6
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?

kahoot!
QUESTION 4 OF 6
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?

kahoot!
QUESTION 5 OF 6
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?

kahoot!
QUESTION 6 OF 6
Karel places balls on a street until hitting a wall. One extra ball is needed at the very end. This is called a…
β–²
A. Syntax error
β—†
B. Infinite loop
●
C. Fencepost bug
β– 
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.
Last updated  2026/03/24 16:17:47 PDTHits  3