From a8c59885a0e59f57d99ae77798d84ac31d987097 Mon Sep 17 00:00:00 2001 From: Charles Mensah Date: Fri, 28 Nov 2025 06:39:24 +0100 Subject: [PATCH] solved lab --- .../lab-python-functions-checkpoint.ipynb | 250 ++++++++++++++++++ lab-python-functions.ipynb | 187 ++++++++++++- 2 files changed, 434 insertions(+), 3 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..1aad3ee --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,250 @@ +{ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "71347449-f039-4ad9-af88-22d98d1b7e6d", + "metadata": {}, + "outputs": [], + "source": [ + "#Managing Customer Orders with Functions\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative. Try again.\")\n", + " else:\n", + " inventory [product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7c1019fd-9bfb-44d0-be6a-aad717479087", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " print('\\nEnter product names for the customer order(type \"done\" to finish):')\n", + " \n", + " while True:\n", + " product = input(\"Product name: \").lower()\n", + " if product == \"done\":\n", + " break\n", + " customer_orders.add(product)\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ddbc8b2c-b052-42e2-aea4-f7ba44325fdb", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is out of stock.\")\n", + " else:\n", + " print(f\"{product} is not sold in this store.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ee571327-c5eb-4d06-953a-994c99925e97", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_ordered = len(customer_orders)\n", + " unique_products = len(products)\n", + " percentage_unique = (total_ordered / unique_products) * 100\n", + "\n", + " return total_ordered, percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a4cd77eb-0a52-4140-b991-0eec8812a6d3", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage_unique = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_unique:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "eb30bfdc-fdfa-42d8-b5a2-6b659aaf9106", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0b00d2a1-687f-4403-bd08-0fde7be1de92", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 4\n", + "Enter quantity available for mug: 6\n", + "Enter quantity available for hat: 4\n", + "Enter quantity available for keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter product names for the customer order(type \"done\" to finish):\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product name: \"t-shirt\"\n", + "Product name: mug\n", + "Product name: hat\n", + "Product name: book\n", + "Product name: keychain\n", + "Product name: done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\"t-shirt\" is not sold in this store.\n", + "book is not sold in this store.\n", + "\n", + "Order Statistics:\n", + "Total products ordered: 5\n", + "Percentage of unique products ordered: 125.00%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 4\n", + "mug: 5\n", + "hat: 3\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "#### inventory = {}\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66f90397-8076-42e9-86a4-a9e28391520e", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..1aad3ee 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,194 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "71347449-f039-4ad9-af88-22d98d1b7e6d", + "metadata": {}, + "outputs": [], + "source": [ + "#Managing Customer Orders with Functions\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative. Try again.\")\n", + " else:\n", + " inventory [product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7c1019fd-9bfb-44d0-be6a-aad717479087", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " print('\\nEnter product names for the customer order(type \"done\" to finish):')\n", + " \n", + " while True:\n", + " product = input(\"Product name: \").lower()\n", + " if product == \"done\":\n", + " break\n", + " customer_orders.add(product)\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ddbc8b2c-b052-42e2-aea4-f7ba44325fdb", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is out of stock.\")\n", + " else:\n", + " print(f\"{product} is not sold in this store.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ee571327-c5eb-4d06-953a-994c99925e97", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_ordered = len(customer_orders)\n", + " unique_products = len(products)\n", + " percentage_unique = (total_ordered / unique_products) * 100\n", + "\n", + " return total_ordered, percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a4cd77eb-0a52-4140-b991-0eec8812a6d3", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage_unique = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_unique:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "eb30bfdc-fdfa-42d8-b5a2-6b659aaf9106", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0b00d2a1-687f-4403-bd08-0fde7be1de92", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 4\n", + "Enter quantity available for mug: 6\n", + "Enter quantity available for hat: 4\n", + "Enter quantity available for keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter product names for the customer order(type \"done\" to finish):\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product name: \"t-shirt\"\n", + "Product name: mug\n", + "Product name: hat\n", + "Product name: book\n", + "Product name: keychain\n", + "Product name: done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\"t-shirt\" is not sold in this store.\n", + "book is not sold in this store.\n", + "\n", + "Order Statistics:\n", + "Total products ordered: 5\n", + "Percentage of unique products ordered: 125.00%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 4\n", + "mug: 5\n", + "hat: 3\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "#### inventory = {}\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66f90397-8076-42e9-86a4-a9e28391520e", + "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": { @@ -61,7 +242,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,