Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn While Loops Practice | Loops
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to Python

bookWhile Loops Practice

Example Application

Imagine you're managing the milk stock in a grocery store. You need to ensure the stock doesn't drop below a certain level, and when it does, you should restock to maintain the stock level.

A while loop is useful in this situation because it allows you to restock milk in increments over several iterations until the stock reaches the desired level. Without a while loop, you'd need to calculate the exact amount of milk to restock all at once.

For instance, consider that a store worker can only restock a fixed amount of milk in one trip (e.g., 20 packs per restock). If the stock isn't fully replenished, the worker returns to the warehouse to bring another batch of milk.

123456789101112131415161718
# Initial amount of milk in stock milk_stock = 15 # Minimum stock level before restocking is necessary min_stock = 50 # Quantity a worker can restock at one time restock_quantity = 20 # Start the loop to restock milk until the stock exceeds the minimum required level while milk_stock < min_stock: # If the loop is running, the condition is `True`, indicating we need more milk print(f"Milk stock is low: {milk_stock} units remaining.") # Simulate the process of restocking milk print("Restocking milk...") # Increase the stock by the quantity the worker can bring in one trip milk_stock += restock_quantity # Output the final stock level after restocking is complete print(f"Milk stock updated: {milk_stock} units, which is now sufficient.")
copy
Note
Note

milk_stock += restock_quantity is equivalent to milk_stock = milk_stock + restock_quantity. It's simply a more concise and readable way to express the same operation.

The flowchart above illustrates the logic behind the while loop used for managing the milk inventory. Notice how the counter variable milk_stock, starting at 15, is incremented by the restock_quantity with each loop iteration.

The loop keeps running until milk_stock surpasses the min_stock threshold of 50, at which point it stops.

Task

Swipe to start coding

You're responsible for keeping apples in stock at your store. Use a while loop to make sure the apple stock reaches at least the minimum required level.

  • Use a while loop to restock apples until apple_stock is greater than or equal to min_apple_stock.
  • In each iteration, print a message like Restocking apples... and increase apple_stock by restock_amount.
  • After the loop ends, print the final apple stock using the provided print statement.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookWhile Loops Practice

Swipe to show menu

Example Application

Imagine you're managing the milk stock in a grocery store. You need to ensure the stock doesn't drop below a certain level, and when it does, you should restock to maintain the stock level.

A while loop is useful in this situation because it allows you to restock milk in increments over several iterations until the stock reaches the desired level. Without a while loop, you'd need to calculate the exact amount of milk to restock all at once.

For instance, consider that a store worker can only restock a fixed amount of milk in one trip (e.g., 20 packs per restock). If the stock isn't fully replenished, the worker returns to the warehouse to bring another batch of milk.

123456789101112131415161718
# Initial amount of milk in stock milk_stock = 15 # Minimum stock level before restocking is necessary min_stock = 50 # Quantity a worker can restock at one time restock_quantity = 20 # Start the loop to restock milk until the stock exceeds the minimum required level while milk_stock < min_stock: # If the loop is running, the condition is `True`, indicating we need more milk print(f"Milk stock is low: {milk_stock} units remaining.") # Simulate the process of restocking milk print("Restocking milk...") # Increase the stock by the quantity the worker can bring in one trip milk_stock += restock_quantity # Output the final stock level after restocking is complete print(f"Milk stock updated: {milk_stock} units, which is now sufficient.")
copy
Note
Note

milk_stock += restock_quantity is equivalent to milk_stock = milk_stock + restock_quantity. It's simply a more concise and readable way to express the same operation.

The flowchart above illustrates the logic behind the while loop used for managing the milk inventory. Notice how the counter variable milk_stock, starting at 15, is incremented by the restock_quantity with each loop iteration.

The loop keeps running until milk_stock surpasses the min_stock threshold of 50, at which point it stops.

Task

Swipe to start coding

You're responsible for keeping apples in stock at your store. Use a while loop to make sure the apple stock reaches at least the minimum required level.

  • Use a while loop to restock apples until apple_stock is greater than or equal to min_apple_stock.
  • In each iteration, print a message like Restocking apples... and increase apple_stock by restock_amount.
  • After the loop ends, print the final apple stock using the provided print statement.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 4
single

single

some-alt