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
119 changes: 115 additions & 4 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": []
},
Expand All @@ -12,7 +11,6 @@
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
Expand All @@ -37,6 +35,119 @@
"\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": 70,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter t-shirt quantity: 1\n",
"Enter mug quantity: 1\n",
"Enter hat quantity: 1\n",
"Enter book quantity: 1\n",
"Enter keychain quantity: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory: {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"What would you like to order: t-shirt, mug, hat, book or keychain? mug\n",
"Do you want to add another product? (yes/no) yes\n",
"What would you like to order: t-shirt, mug, hat, book or keychain? hat\n",
"Do you want to add another product? (yes/no) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer order: {'hat', 'mug'}\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0\n",
"\n",
"Inventory: {'t-shirt': 1, 'mug': 0, 'hat': 0, 'book': 1, 'keychain': 1}\n",
"\n",
"T-Shirt: 1\n",
"Mug: 0\n",
"Hat: 0\n",
"Book: 1\n",
"Keychain: 1\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"for item in products:\n",
" quantity = int(input(f\"Enter {item} quantity: \"))\n",
" inventory[item] = quantity\n",
"\n",
"print(\"Inventory:\", inventory)\n",
"\n",
"print(\"\")\n",
"\n",
"customer_orders = set()\n",
"\n",
"add_product = \"yes\"\n",
"\n",
"while add_product == \"yes\":\n",
" item = input(\"What would you like to order: t-shirt, mug, hat, book or keychain? \").lower()\n",
"\n",
" if item in products:\n",
" customer_orders.add(item)\n",
" else:\n",
" print(\"Invalid product or wrongly written. Please choose one from the list.\")\n",
"\n",
" add_product = input(\"Do you want to add another product? (yes/no) \").lower()\n",
"\n",
"print(\"Customer order: \" + str(customer_orders))\n",
"\n",
"print(\"\")\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n",
"\n",
"order_status = (\n",
" total_products_ordered,\n",
" percentage_products_ordered,)\n",
"\n",
"order_status\n",
"\n",
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", total_products_ordered)\n",
"print(\"Percentage of Products Ordered:\", percentage_products_ordered)\n",
"\n",
"print(\"\")\n",
"\n",
"customer_orders\n",
"\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"Inventory:\", inventory)\n",
"\n",
"print(\"\")\n",
"\n",
"for item, quantity in inventory.items():\n",
" print(item.title() + \":\", quantity)"
]
}
],
"metadata": {
Expand All @@ -55,9 +166,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
"nbformat_minor": 4
}