knowledge-kitchen / exercises

Loops Pair Exercise: for vs. while

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:

Use one computer. Talk through your reasoning out loud — most of the learning happens in the conversation, not the typing.

Setup

  1. Create a folder named loops-exercise.
  2. Inside it, create one file per task: task1.py, task2.py, and so on.
  3. 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.

  1. First, do it with a for loop using range().
  2. Then, below it, do it again with a while loop 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:


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.

  1. Write it with a for loop that iterates over the list.
  2. Write it again with a while loop that uses an index to reach each item (numbers[i]).

Discuss and write your answer as a comment:


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:


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 while loop, 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:


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 for loop and a while loop can both solve the same problem, how do you decide which one to use?

What to submit

Submit your loops-exercise folder containing:

Make sure your discussion comments are included — they count just as much as the working code.