Challenge: Pricing Adjustment Capstone
You are managing a grocery store's system, and you need to maintain decision-making for the inventory, track prices, and perform checks to determine if actions like restocking or removing items from the inventory are needed based on their price or stock.
Swipe to start coding
Manage a grocery inventory using a dictionary in Python. Perform operations like updating prices, adding new items, and managing stock levels based on conditions.
-
Complete the Dictionary: Define
grocery_inventory
with the following items and their details:"Milk"
:("Dairy", 3.50, 8)
"Eggs"
:("Dairy", 5.50, 30)
"Bread"
:("Bakery", 2.99, 15)
"Apples"
:("Produce", 1.50, 50)
-
Check and Update Price:
- Retrieve the price of
"Eggs"
. If the price is greater than $5, print"Eggs are too expensive, reducing the price by $1."
and reduce the price by $1. Otherwise, print"The price of Eggs is reasonable."
.
- Retrieve the price of
-
Add a New Item:
- Add
"Tomatoes"
with details: category"Produce"
, price $1.20, and stock30
.
- Add
-
Manage Stock:
- Check the stock of
"Milk"
. If it's less than10
, print"Milk needs to be restocked. Increasing stock by 20 units."
and increase the stock by20
. Otherwise, print"Milk has sufficient stock."
.
- Check the stock of
-
Remove Item Based on Price:
- Check the price of
"Apples"
. If it exceeds $2, remove"Apples"
from the inventory and print"Apples removed from inventory due to high price."
.
- Check the price of
Output Requirements
-
When checking
"Eggs"
' price, print:"Eggs are too expensive, reducing the price by $1."
if the price is greater than $5."The price of Eggs is reasonable."
if the price is $5 or less.
-
After adding
"Tomatoes"
, print the updated inventory:"Inventory after adding Tomatoes: <$grocery_inventory>"
. -
When checking
"Milk"
stock, print:"Milk needs to be restocked. Increasing stock by 20 units."
if the stock is less than10
."Milk has sufficient stock."
if the stock is10
or more.
-
If
"Apples"
are removed, print"Apples removed from inventory due to high price."
. -
Finally, print the updated inventory:
"Updated inventory: <$grocery_inventory>"
.
Note
Use square brackets to access values from tuples in the dictionary, e.g.,
inventory["Bread"][1]
to get the price of"Bread"
.
Requirements checklist
- Check that after execution, the value of
grocery_inventory["Eggs"][1]
is exactly $4.50 (i.e., $5.50 - $1), since the original price was $5.50 and should be reduced by $1. - Check that
grocery_inventory
contains a key"Tomatoes"
with value("Produce", 1.20, 30)
after execution. - Check that after execution, the value of
grocery_inventory["Milk"][2]
is exactly 28 (i.e., 8 + 20), since the original stock was 8 and should be increased by 20. - Check that
"Apples"
is still present ingrocery_inventory
after execution and its value is unchanged, since its price ($1.50) does not exceed $2. - Check that the final value of
grocery_inventory["Bread"]
is unchanged and equals("Bakery", 2.99, 15)
. - Check that the final value of
grocery_inventory["Milk"][1]
is still $3.50 (the price of milk is not changed). - Check that the final value of
grocery_inventory["Eggs"][0]
is"Dairy"
(the category is unchanged). - Check that the final value of
grocery_inventory["Tomatoes"][2]
is 30 (the stock of tomatoes is correct).
Solution
Thanks for your feedback!