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
200 changes: 198 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,207 @@
"\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": 1,
"id": "fa61c028",
"metadata": {},
"outputs": [],
"source": [
"# 1. Define the products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a9d4ff60",
"metadata": {},
"outputs": [],
"source": [
"# 2. Create the inventory dictionary\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "456eae9f",
"metadata": {},
"outputs": [],
"source": [
"# 3. Ask for the quantity of each product\n",
"for product in products:\n",
" quantity = int(input(f\"How many {product}s are available? \"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bda7950a",
"metadata": {},
"outputs": [],
"source": [
"# 4. Create an empty set for customer orders\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "669d4b92",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt added to the order.\n",
"mug added to the order.\n",
"hat added to the order.\n"
]
}
],
"source": [
"# 5. Keep asking for products until the customer says no\n",
"while True:\n",
" product = input(\n",
" \"Enter a product to order \"\n",
" \"(t-shirt, mug, hat, book, keychain): \"\n",
" ).lower()\n",
"\n",
" # Check whether the product exists\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" print(f\"{product} added to the order.\")\n",
" else:\n",
" print(\"This product is not available.\")\n",
"\n",
" another_product = input(\n",
" \"Do you want to add another product? (yes/no): \"\n",
" ).lower()\n",
"\n",
" if another_product == \"no\":\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "961c7434",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Products in customer order:\n",
"{'mug', 'hat', 't-shirt'}\n"
]
}
],
"source": [
"# 6. Print the ordered products\n",
"print(\"\\nProducts in customer order:\")\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "56cccf5c",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 7. Calculate order statistics\n",
"total_products_ordered = len(customer_orders)\n",
"\n",
"percentage_ordered = (\n",
" total_products_ordered / len(products)\n",
") * 100\n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e5e627a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"# 8. Print the order statistics\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "68edad68",
"metadata": {},
"outputs": [],
"source": [
"# 9. Update only the inventory of ordered products\n",
"for product in customer_orders:\n",
" inventory[product] -= 1\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "cdc4c370",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Updated Inventory:\n",
"t-shirt: 19\n",
"mug: 9\n",
"hat: 9\n",
"book: 50\n",
"keychain: 100\n"
]
}
],
"source": [
"# 10. Print the updated inventory\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product in products:\n",
" print(f\"{product}: {inventory[product]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4d6fadb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +251,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down