Skip to content
Open
Show file tree
Hide file tree
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
69 changes: 69 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"\n",
"6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"Hints for functions:\n",
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added anaconda_projects/db/project_filebrowser.db
Binary file not shown.
274 changes: 271 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,281 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "daac5089-c8e6-4e67-a86a-d97d1d76506c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Inventory Initialization ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter starting quantity for 'Laptop': 10\n",
"Enter starting quantity for 'Mouse': 12\n",
"Enter starting quantity for 'Keyboard': 14\n",
"Enter starting quantity for 'Monitor': 16\n",
"Enter starting quantity for 'Webcam': 18\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Customer Order Entry ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order (or type 'done' to finish): finish\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'finish' is not an available product. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order (or type 'done' to finish): Mouse\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'Mouse' added to the order set.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order (or type 'done' to finish): Webcam\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'Webcam' added to the order set.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order (or type 'done' to finish): Monitor\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'Monitor' added to the order set.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order (or type 'done' to finish): done\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Updating Inventory ---\n",
"Inventory reduced for 'Webcam'. New quantity: 17\n",
"Inventory reduced for 'Monitor'. New quantity: 15\n",
"Inventory reduced for 'Mouse'. New quantity: 11\n",
"\n",
"--- Order Statistics ---\n",
"Total unique products ordered: **3**\n",
"Percentage of unique available products ordered: **60.00%**\n",
"\n",
"--- Final Updated Inventory ---\n",
"**Laptop**: 10\n",
"**Mouse**: 11\n",
"**Keyboard**: 14\n",
"**Monitor**: 15\n",
"**Webcam**: 17\n"
]
}
],
"source": [
"# Initial list of available product names\n",
"AVAILABLE_PRODUCTS = [\"Laptop\", \"Mouse\", \"Keyboard\", \"Monitor\", \"Webcam\"]\n",
"\n",
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Initializes the inventory dictionary by prompting the user for the \n",
" starting quantity of each product.\n",
" \n",
" :param products: A list of product names (strings).\n",
" :return: A dictionary where keys are product names and values are \n",
" their initial quantities (integers).\n",
" \"\"\"\n",
" inventory = {}\n",
" print(\"\\n--- Inventory Initialization ---\")\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" # Prompt user for quantity and ensure it's a non-negative integer\n",
" quantity = int(input(f\"Enter starting quantity for '{product}': \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" break\n",
" else:\n",
" print(\"Quantity cannot be negative. Please enter a positive number.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a whole number.\")\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"\n",
" Prompts the user to enter product names for an order using a loop.\n",
" \n",
" :return: A set of unique product names ordered by the customer.\n",
" \"\"\"\n",
" customer_orders = set()\n",
" print(\"\\n--- Customer Order Entry ---\")\n",
" while True:\n",
" # a. Prompt the user to enter the name of a product\n",
" product_name = input(\"Enter a product to order (or type 'done' to finish): \").strip()\n",
" \n",
" if product_name.lower() == 'done':\n",
" break\n",
" \n",
" # Optional: Basic validation to check if the product is in the master list\n",
" if product_name in AVAILABLE_PRODUCTS:\n",
" # b. Add the product name to the \"customer_orders\" set\n",
" customer_orders.add(product_name)\n",
" print(f\"'{product_name}' added to the order set.\")\n",
" else:\n",
" print(f\"'{product_name}' is not an available product. Please try again.\")\n",
"\n",
" # c. Ask the user if they want to add another product (now implicit in 'done' break)\n",
" \n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Updates the inventory by subtracting 1 from the quantity of each product \n",
" that was ordered.\n",
" \n",
" :param customer_orders: The set of products ordered by the customer.\n",
" :param inventory: The dictionary representing current stock levels.\n",
" \"\"\"\n",
" print(\"\\n--- Updating Inventory ---\")\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" # Subtract 1 from the quantity for each unique ordered product\n",
" inventory[product] -= 1\n",
" print(f\"Inventory reduced for '{product}'. New quantity: {inventory[product]}\")\n",
" elif product in inventory and inventory[product] == 0:\n",
" print(f\"Warning: '{product}' was ordered but is out of stock.\")\n",
" else:\n",
" # Should not happen if validation in get_customer_orders is good\n",
" print(f\"Error: Ordered product '{product}' not found in inventory.\")\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" Calculates the total number of unique products ordered and the percentage \n",
" of unique products ordered relative to the total number of available products.\n",
" \n",
" :param customer_orders: The set of unique products ordered.\n",
" :param products: The list of all available products.\n",
" :return: A tuple containing (total_products_ordered, percentage_ordered).\n",
" \"\"\"\n",
" total_products_ordered = len(customer_orders)\n",
" total_available_products = len(products)\n",
" \n",
" if total_available_products > 0:\n",
" # Calculate percentage: (unique ordered / total available) * 100\n",
" percentage_ordered = (total_products_ordered / total_available_products) * 100\n",
" else:\n",
" percentage_ordered = 0.0\n",
" \n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" Prints the calculated order statistics in a formatted way.\n",
" \n",
" :param order_statistics: A tuple containing (total_products_ordered, percentage_ordered).\n",
" \"\"\"\n",
" total_ordered, percentage = order_statistics\n",
" \n",
" print(\"\\n--- Order Statistics ---\")\n",
" print(f\"Total unique products ordered: **{total_ordered}**\")\n",
" print(f\"Percentage of unique available products ordered: **{percentage:.2f}%**\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Prints the final state of the inventory dictionary.\n",
" \n",
" :param inventory: The dictionary representing the final stock levels.\n",
" \"\"\"\n",
" print(\"\\n--- Final Updated Inventory ---\")\n",
" # Use a loop for clean, key-value printing\n",
" for product, quantity in inventory.items():\n",
" status = \" (LOW STOCK)\" if quantity < 5 else \"\"\n",
" print(f\"**{product}**: {quantity}{status}\")\n",
"\n",
"# --- Main Program Execution ---\n",
"\n",
"# 1. Initialize Inventory\n",
"inventory_data = initialize_inventory(AVAILABLE_PRODUCTS)\n",
"\n",
"# 2. Get Customer Orders\n",
"orders_set = get_customer_orders()\n",
"\n",
"# 3. Update Inventory\n",
"update_inventory(orders_set, inventory_data)\n",
"\n",
"# 4. Calculate Statistics\n",
"stats = calculate_order_statistics(orders_set, AVAILABLE_PRODUCTS)\n",
"\n",
"# 5. Print Statistics\n",
"print_order_statistics(stats)\n",
"\n",
"# 6. Print Updated Inventory\n",
"print_updated_inventory(inventory_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1240279-8751-448f-b5fd-e9c00f67b9b4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -61,7 +329,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down