Loops Pair Exercise: for vs. while
- Overview
- How to work together (Pair Programming)
- Setup
- Task 1 — Count to 10 two ways (10 min)
- Task 2 — Sum a list (10 min)
- Task 3 — Keep asking until valid (15 min)
- Task 4 — Running total with a sentinel (15 min)
- Wrap-up: for vs. while (10 min)
- What to submit
Overview
Work in teams of two. You will write several short Python programs that
use loops, then compare how for and while loops behave on the same task.
The goal is not just to make the programs work, but to understand when each
kind of loop is the right tool.
Plan for about one hour. See the Loops recap if you need
a refresher on for loops and while loops.
How to work together (Pair Programming)
You will use the driver / navigator method:
- The driver types the code.
- The navigator reads the instructions, thinks about the approach, and reviews what the driver writes.
- Swap roles after every task. Both partners must take a turn driving.
Use one computer. Talk through your reasoning out loud — most of the learning happens in the conversation, not the typing.
Setup
- Create a folder named
loops-exercise. - Inside it, create one file per task:
task1.py,task2.py, and so on. - At the top of each file, add a comment with both partners’ names.
Task 1 — Count to 10 two ways (10 min)
Driver: Partner A
Write a program that prints the numbers 1 through 10, one per line.
- First, do it with a
forloop usingrange(). - Then, below it, do it again with a
whileloop and a counter variable.
Both versions should produce the exact same output.
Discuss and write your answer as a comment at the bottom of the file:
- What does the
whileloop version need that theforloop version does not? - What would happen to the
whileloop if you forgot to increase the counter?
Task 2 — Sum a list (10 min)
Driver: Partner B
Given this list:
numbers = [4, 8, 15, 16, 23, 42]
Use the accumulator pattern to calculate and print the total of all the numbers.
- Write it with a
forloop that iterates over the list. - Write it again with a
whileloop that uses an index to reach each item (numbers[i]).
Discuss and write your answer as a comment:
- Which version was easier to write? Why?
- When you already know the exact sequence of values, which loop is the more natural choice?
Task 3 — Keep asking until valid (15 min)
Driver: Partner A
Write a program that asks the user to enter a positive number. If the user enters something that is not a positive number, keep asking until they get it right. Then print the number.
Hint: this is the input validation pattern from the recap. Think about whether you know in advance how many times you’ll need to ask.
Discuss and write your answer as a comment:
- Could you reasonably solve this with a
forloop? Why or why not? - This is an example of an indefinite loop. What makes it indefinite?
Task 4 — Running total with a sentinel (15 min)
Driver: Partner B
Write a program that repeatedly asks the user to enter a number and keeps a
running total. When the user types done, stop asking and print the final
total.
Hint: use a
whileloop, the accumulator pattern, and a sentinel value ("done") to know when to stop. Look at the Running total example in the recap if you get stuck.
Discuss and write your answer as a comment:
- Why is a
whileloop necessary here instead of aforloop?
Wrap-up: for vs. while (10 min)
Together, fill in this comparison table in a new file called summary.md or as
comments in a summary.py file:
| Question |
for loop |
while loop |
|---|---|---|
| Do you know the number of repetitions in advance? | ||
| What controls when the loop stops? | ||
| Best used for definite or indefinite loops? | ||
| Give one example task from today that suits it best |
Then write two or three sentences answering:
If a
forloop and awhileloop can both solve the same problem, how do you decide which one to use?
What to submit
Submit your loops-exercise folder containing:
-
task1.py,task2.py,task3.py,task4.py(each with both names at top) - Your
summaryfile with the completed table and final reflection
Make sure your discussion comments are included — they count just as much as the working code.