Conditional looping with lists
Before you scale your tomato and basil pasta recipe for the party, you need to inspect the ingredient quantities to understand what you're working with. Your recipe quantities are stored in a list called quantities (measured in grams). As a first step in building your recipe scaler, you'll loop through these quantities and use conditionals to categorize them - but you won't scale them yet. This helps you understand the ingredient distribution before making any changes.
This exercise is part of the course
Introduction to Python for Developers
Exercise instructions
- Loop through each quantity in the
quantitieslist usingqtyas your iterator variable. - Inside the loop, add a conditional that checks if the
qtyis greater than or equal to 400 grams. If so, print'Large quantity'. - Add an
elifcondition to check if theqtyis greater than or equal to 200 grams. If so, print'Medium quantity'. - Add an
elseclause to handle all remaining quantities and print'Small quantity'.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
quantities = [500, 400, 20, 15, 15, 7]
# Loop through each quantity in the recipe
for ____ in ____:
# Check if it's a large quantity (400g or more)
____ ____ >= 400:
print('Large quantity')
# Check if it's a medium quantity (200g or more)
____ qty >= ____:
print('Medium quantity')
# Otherwise it's a small quantity
____:
print('Small quantity')