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
202 changes: 199 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,209 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "124df5a7-007b-416d-aeec-7a892f94e6e1",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ce1ef732-1998-43af-9c4f-e97a28100275",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "f0e78b2f-de51-45f1-9fc6-cc484d1393c8",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" product = input(\n",
" \"Enter a product name or type 'stop' to finish: \"\n",
" ).lower()\n",
"\n",
" if product in products:\n",
" customer_orders.add(product)\n",
"\n",
" elif product == \"stop\":\n",
" print(\"Ordering finished.\")\n",
" break\n",
"\n",
" else:\n",
" print(\"This product is not available.\")\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "3cd0df25-5720-45b4-92ea-3887ffb72e5b",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders,inventory):\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
"\n",
" return inventory\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1c51f961-d734-4854-a015-bf88ef386d5c",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
"\n",
" percentage_products_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
" return (total_products_ordered, percentage_products_ordered)\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "b7120318-05ea-4cbc-88ef-f1fd02b72d0e",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics ):\n",
" print (\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(\n",
" f\"Percentage of Unique Products Ordered: \"\n",
" f\"{order_statistics[1]:.2f}%\")\n",
"\n",
" \n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "aa777fe3-09f5-462e-8572-7f1db17c0c34",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
"\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "e307b1d0-57d4-43f5-98db-4c9b3e1eb444",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
"\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "9e515355-28ad-4053-ab80-e81703b40837",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt: 5\n",
"Enter the quantity of mug: 4\n",
"Enter the quantity of hat: 6\n",
"Enter the quantity of book: 2\n",
"Enter the quantity of keychain: 1\n",
"Enter a product name or type 'stop' to finish: stop\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ordering finished.\n",
"Order Statistics:\n",
"Total Products Ordered: 0\n",
"Percentage of Unique Products Ordered: 0.00%\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 6\n",
"book: 2\n",
"keychain: 1\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(\n",
" customer_orders,\n",
" products\n",
")\n",
"\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2846d308-f913-4d34-b246-3351ea50734f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "pj1_env",
"language": "python",
"name": "python3"
"name": "venv"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -61,7 +257,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.13"
}
},
"nbformat": 4,
Expand Down