0
0
Pythonprogramming~15 mins

Nested while loops in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Nested while loops
📖 Scenario: You are helping a small bakery organize their daily baking schedule. They bake different types of bread in batches. Each batch has a fixed number of loaves. You want to print out the baking plan showing each batch and the loaves inside it.
🎯 Goal: Build a program that uses nested while loops to print the batch number and loaf number for each loaf baked.
📋 What You'll Learn
Create a variable batches with the value 3 representing the number of batches.
Create a variable loaves_per_batch with the value 4 representing loaves in each batch.
Use a nested while loop: the outer loop counts batches, the inner loop counts loaves.
Print the batch number and loaf number in the format: Batch 1, Loaf 1.
💡 Why This Matters
🌍 Real World
Bakers and factory workers often need to organize tasks in batches and steps, so nested loops help automate schedules and reports.
💼 Career
Understanding nested loops is essential for programming tasks that involve multi-level data or repeated actions inside other repeated actions, common in software development and automation.
Progress0 / 4 steps
1
Set up batch and loaf counts
Create a variable called batches and set it to 3. Create another variable called loaves_per_batch and set it to 4.
Python
Need a hint?

Use simple assignment like batches = 3 and loaves_per_batch = 4.

2
Initialize batch counter
Create a variable called batch and set it to 1 to start counting batches.
Python
Need a hint?

Start counting batches from 1 using batch = 1.

3
Write nested while loops for batches and loaves
Write a while loop that runs while batch <= batches. Inside it, create a variable loaf set to 1. Then write another while loop inside that runs while loaf <= loaves_per_batch. Inside the inner loop, print Batch {batch}, Loaf {loaf} using an f-string, then increase loaf by 1. After the inner loop ends, increase batch by 1.
Python
Need a hint?

Remember to reset loaf to 1 inside the outer loop before the inner loop starts.

4
Run the program to see the baking plan
Run the program to print the baking plan showing each batch and loaf number as Batch 1, Loaf 1 up to Batch 3, Loaf 4.
Python
Need a hint?

Check that the output lists all batches and loaves correctly.