Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,74 @@
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b2271121",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of inventory: {'t-shirt': 45, 'mug': 87, 'hat': 90, 'book': 23, 'keychain': 44}\n",
"All products added to the order.\n",
"Customer order: {'mug', 'book'}\n",
"Updated inventory:\n",
"t-shirt : 45\n",
"mug : 86\n",
"hat : 90\n",
"book : 22\n",
"keychain : 44\n"
]
}
],
"source": [
"#3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\").\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"#Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity #add to the dictionairy: key = assign the value\n",
"\n",
"print(\"List of inventory: \", inventory)\n",
"\n",
"customer_order = set()\n",
"\n",
"while True:\n",
" p = input(\"Enter the product you want to order: \")\n",
" customer_order.add(p)\n",
"\n",
" user_input = input(\"Do you want to add another product? (yes/no): \").lower()\n",
"\n",
" if user_input == \"yes\":\n",
" continue\n",
" else:\n",
" print(\"All products added to the order.\")\n",
" break\n",
"\n",
"print(\"Customer order:\", customer_order)\n",
"\n",
"for product in customer_order:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" else:\n",
" print({product}, \"is not in the inventory.\")\n",
"\n",
"print(\"Updated inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(product, \":\", quantity)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +118,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down