diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..fa0fd27 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,694 @@ +{ + "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": 157, + "id": "2311d549-5f47-4303-9008-5524c39c8063", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 158, + "id": "842def40-e4b1-4d17-ba01-572296d91c29", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named initialize_inventory that takes products as a parameter. \n", + "#Inside the function, implement the code for initializing the inventory dictionary using a loop and user input." + ] + }, + { + "cell_type": "code", + "execution_count": 159, + "id": "07abc7a6-49e8-40f5-a610-ba0b720e0d27", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " \n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "id": "592b24e9-1af2-40cc-98b3-31d7d00855dd", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 5\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 5\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 5\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 161, + "id": "55f063e4-da82-4730-afda-53e959de6119", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory:\n", + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "print(\"\\nInventory:\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 162, + "id": "f3bfa32c-e51f-4437-a08a-e8ae2d51465e", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named get_customer_orders that takes no parameters. \n", + "#Inside the function, implement the code for prompting the user to enter the product names using a loop. \n", + "#The function should return the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 163, + "id": "fbbbd72c-e8bb-4afb-a55b-77ecaedbe1ea", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + "\n", + " customer_orders = {}\n", + "\n", + " while True:\n", + " # Prompt user for product name\n", + " product_name = input(\"Enter the name of a product to order: \").strip().lower()\n", + "\n", + " # Check if the product is in inventory\n", + " if product_name in inventory:\n", + " quantity = int(input(f\"Enter the quantity for {product_name}: \"))\n", + " if quantity <= inventory[product_name]:\n", + " customer_orders[product_name] = quantity\n", + " print(f\"{quantity} of {product_name} has been added to your order.\")\n", + " else:\n", + " print(f\"Sorry, we only have {inventory[product_name]} of {product_name} available.\")\n", + " else:\n", + " print(f\"Sorry, {product_name} is not available in the inventory.\")\n", + "\n", + " # Ask if they want to add another product\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != 'yes':\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 164, + "id": "68519a75-4738-4a02-b538-cbc7ae085d48", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n", + "Enter the quantity for mug: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 of mug has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: book\n", + "Enter the quantity for book: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we only have 5 of book available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: keychain\n", + "Enter the quantity for keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 of keychain has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 165, + "id": "7bde05a4-2d8c-4464-ac3f-5f0986968e62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 166, + "id": "e9f1eff9-4fed-4c37-9c9e-b95f6e92df3b", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named update_inventory that takes customer_orders and inventory as parameters. \n", + "#Inside the function, implement the code for updating the inventory dictionary based on the customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 167, + "id": "d645bd41-91f6-462d-ad7c-d200b910776b", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory (customer_orders, inventory):\n", + " \n", + " for product, quantity in customer_orders.items():\n", + " if product in inventory:\n", + " inventory[product] -= quantity\n", + " print(f\"Updated inventory: {product} now has {inventory[product]} left.\")\n", + " else:\n", + " print(f\"Error: {product} is not in the inventory.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 168, + "id": "b671b3dc-fbba-4403-bbea-268784921f4e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: mug now has 3 left.\n", + "Updated inventory: keychain now has 4 left.\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "id": "c33ffedc-2d0e-4026-9442-f1d5b8e155ed", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n", + "#Inside the function, implement the code for calculating the order statistics \n", + "#(total products ordered, and percentage of unique products ordered). The function should return these values." + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "id": "ce37b930-29b0-4c1d-bef3-6329d04f588e", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics (customer_orders, products):\n", + " \n", + " total_products_ordered = sum(customer_orders.values())\n", + " \n", + " unique_products_ordered = len(customer_orders)\n", + " \n", + " total_unique_products = len(products)\n", + " percentage_of_unique_products_ordered = (unique_products_ordered / total_unique_products) * 100 if total_unique_products > 0 else 0\n", + "\n", + " return total_products_ordered, percentage_of_unique_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "id": "e5a0a15e-755d-4fe6-9f2c-99ef04e1ac50", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistic = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 172, + "id": "d33a1341-b5ea-48cc-8ead-fea8d5e4a4ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 40.0)\n" + ] + } + ], + "source": [ + "print(order_statistic)" + ] + }, + { + "cell_type": "code", + "execution_count": 173, + "id": "40e4b8d0-c269-417e-9f1b-7c8382e2fefa", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named print_order_statistics that takes order_statistics as a parameter. \n", + "#Inside the function, implement the code for printing the order statistics." + ] + }, + { + "cell_type": "code", + "execution_count": 178, + "id": "d0a8a4e5-1d13-4e43-bdbc-e5c3d69c0505", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistic):\n", + " total_products_ordered, percentage_of_unique_products_ordered = order_statistic\n", + " \n", + " print(f\"Total number of ordered products: {total_products_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_of_unique_products_ordered: .2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 179, + "id": "119b17e4-a8ad-46ac-b65f-0d745cd526cc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total number of ordered products: 3\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "printed_statistic = print_order_statistics(order_statistic)" + ] + }, + { + "cell_type": "code", + "execution_count": 180, + "id": "f553ecf1-f654-4aff-9a57-37ec12a8de16", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named print_updated_inventory that takes inventory as a parameter. \n", + "#Inside the function, implement the code for printing the updated inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 181, + "id": "ef53ecf6-337e-4a8c-8a41-d9536b795a30", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory (inventory):\n", + "\n", + " print(\"Final updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 182, + "id": "a34e825d-3e5a-480a-bccf-f28c6c75bf30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Final updated Inventory:\n", + "t-shirt: 5\n", + "mug: 3\n", + "hat: 5\n", + "book: 5\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 183, + "id": "cfc87748-f9a9-457e-948f-7d1404e344ad", + "metadata": {}, + "outputs": [], + "source": [ + "#Call the functions in the appropriate sequence to execute the program and manage customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 184, + "id": "92f983ee-e5e4-4505-b67b-bcceb56bf2bd", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 185, + "id": "fae5c34f-247b-4e87-b385-ea77de2eede4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 10\n", + "Enter the quantity for mug: 10\n", + "Enter the quantity for hat: 10\n", + "Enter the quantity for book: 10\n", + "Enter the quantity for keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory:\n", + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "\n", + "print(\"\\nInventory:\")\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 186, + "id": "776ef93d-63a9-457c-91a5-6916616bfb10", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n", + "Enter the quantity for mug: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 of mug has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: book\n", + "Enter the quantity for book: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we only have 10 of book available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: keychain\n", + "Enter the quantity for keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 of keychain has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 187, + "id": "43e8311c-45e3-4d4c-82c0-5d90ba7e51b5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 188, + "id": "bb650eeb-f3b9-4d18-88ab-dce09feeefbe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: mug now has 8 left.\n", + "Updated inventory: keychain now has 9 left.\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 189, + "id": "f14c0708-5525-487c-a103-e8bb1b9570c9", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistic = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 190, + "id": "fffa165e-1610-4b4e-90f0-0253c7f018f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total number of ordered products: 3\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "printed_statistic = print_order_statistics(order_statistic)" + ] + }, + { + "cell_type": "code", + "execution_count": 191, + "id": "3c192067-16a2-4bb5-813a-8beae4efb3f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Final updated Inventory:\n", + "t-shirt: 10\n", + "mug: 8\n", + "hat: 10\n", + "book: 10\n", + "keychain: 9\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f5bce281-1a84-4e9f-93df-f932c308cca7", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..fa0fd27 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,638 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 157, + "id": "2311d549-5f47-4303-9008-5524c39c8063", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 158, + "id": "842def40-e4b1-4d17-ba01-572296d91c29", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named initialize_inventory that takes products as a parameter. \n", + "#Inside the function, implement the code for initializing the inventory dictionary using a loop and user input." + ] + }, + { + "cell_type": "code", + "execution_count": 159, + "id": "07abc7a6-49e8-40f5-a610-ba0b720e0d27", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " \n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "id": "592b24e9-1af2-40cc-98b3-31d7d00855dd", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 5\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 5\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 5\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 161, + "id": "55f063e4-da82-4730-afda-53e959de6119", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory:\n", + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "print(\"\\nInventory:\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 162, + "id": "f3bfa32c-e51f-4437-a08a-e8ae2d51465e", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named get_customer_orders that takes no parameters. \n", + "#Inside the function, implement the code for prompting the user to enter the product names using a loop. \n", + "#The function should return the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 163, + "id": "fbbbd72c-e8bb-4afb-a55b-77ecaedbe1ea", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + "\n", + " customer_orders = {}\n", + "\n", + " while True:\n", + " # Prompt user for product name\n", + " product_name = input(\"Enter the name of a product to order: \").strip().lower()\n", + "\n", + " # Check if the product is in inventory\n", + " if product_name in inventory:\n", + " quantity = int(input(f\"Enter the quantity for {product_name}: \"))\n", + " if quantity <= inventory[product_name]:\n", + " customer_orders[product_name] = quantity\n", + " print(f\"{quantity} of {product_name} has been added to your order.\")\n", + " else:\n", + " print(f\"Sorry, we only have {inventory[product_name]} of {product_name} available.\")\n", + " else:\n", + " print(f\"Sorry, {product_name} is not available in the inventory.\")\n", + "\n", + " # Ask if they want to add another product\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != 'yes':\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 164, + "id": "68519a75-4738-4a02-b538-cbc7ae085d48", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n", + "Enter the quantity for mug: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 of mug has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: book\n", + "Enter the quantity for book: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we only have 5 of book available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: keychain\n", + "Enter the quantity for keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 of keychain has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 165, + "id": "7bde05a4-2d8c-4464-ac3f-5f0986968e62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 166, + "id": "e9f1eff9-4fed-4c37-9c9e-b95f6e92df3b", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named update_inventory that takes customer_orders and inventory as parameters. \n", + "#Inside the function, implement the code for updating the inventory dictionary based on the customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 167, + "id": "d645bd41-91f6-462d-ad7c-d200b910776b", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory (customer_orders, inventory):\n", + " \n", + " for product, quantity in customer_orders.items():\n", + " if product in inventory:\n", + " inventory[product] -= quantity\n", + " print(f\"Updated inventory: {product} now has {inventory[product]} left.\")\n", + " else:\n", + " print(f\"Error: {product} is not in the inventory.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 168, + "id": "b671b3dc-fbba-4403-bbea-268784921f4e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: mug now has 3 left.\n", + "Updated inventory: keychain now has 4 left.\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "id": "c33ffedc-2d0e-4026-9442-f1d5b8e155ed", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n", + "#Inside the function, implement the code for calculating the order statistics \n", + "#(total products ordered, and percentage of unique products ordered). The function should return these values." + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "id": "ce37b930-29b0-4c1d-bef3-6329d04f588e", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics (customer_orders, products):\n", + " \n", + " total_products_ordered = sum(customer_orders.values())\n", + " \n", + " unique_products_ordered = len(customer_orders)\n", + " \n", + " total_unique_products = len(products)\n", + " percentage_of_unique_products_ordered = (unique_products_ordered / total_unique_products) * 100 if total_unique_products > 0 else 0\n", + "\n", + " return total_products_ordered, percentage_of_unique_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "id": "e5a0a15e-755d-4fe6-9f2c-99ef04e1ac50", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistic = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 172, + "id": "d33a1341-b5ea-48cc-8ead-fea8d5e4a4ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 40.0)\n" + ] + } + ], + "source": [ + "print(order_statistic)" + ] + }, + { + "cell_type": "code", + "execution_count": 173, + "id": "40e4b8d0-c269-417e-9f1b-7c8382e2fefa", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named print_order_statistics that takes order_statistics as a parameter. \n", + "#Inside the function, implement the code for printing the order statistics." + ] + }, + { + "cell_type": "code", + "execution_count": 178, + "id": "d0a8a4e5-1d13-4e43-bdbc-e5c3d69c0505", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistic):\n", + " total_products_ordered, percentage_of_unique_products_ordered = order_statistic\n", + " \n", + " print(f\"Total number of ordered products: {total_products_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_of_unique_products_ordered: .2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 179, + "id": "119b17e4-a8ad-46ac-b65f-0d745cd526cc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total number of ordered products: 3\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "printed_statistic = print_order_statistics(order_statistic)" + ] + }, + { + "cell_type": "code", + "execution_count": 180, + "id": "f553ecf1-f654-4aff-9a57-37ec12a8de16", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named print_updated_inventory that takes inventory as a parameter. \n", + "#Inside the function, implement the code for printing the updated inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 181, + "id": "ef53ecf6-337e-4a8c-8a41-d9536b795a30", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory (inventory):\n", + "\n", + " print(\"Final updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 182, + "id": "a34e825d-3e5a-480a-bccf-f28c6c75bf30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Final updated Inventory:\n", + "t-shirt: 5\n", + "mug: 3\n", + "hat: 5\n", + "book: 5\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 183, + "id": "cfc87748-f9a9-457e-948f-7d1404e344ad", + "metadata": {}, + "outputs": [], + "source": [ + "#Call the functions in the appropriate sequence to execute the program and manage customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 184, + "id": "92f983ee-e5e4-4505-b67b-bcceb56bf2bd", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 185, + "id": "fae5c34f-247b-4e87-b385-ea77de2eede4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 10\n", + "Enter the quantity for mug: 10\n", + "Enter the quantity for hat: 10\n", + "Enter the quantity for book: 10\n", + "Enter the quantity for keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory:\n", + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "\n", + "print(\"\\nInventory:\")\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 186, + "id": "776ef93d-63a9-457c-91a5-6916616bfb10", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n", + "Enter the quantity for mug: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 of mug has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: book\n", + "Enter the quantity for book: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we only have 10 of book available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product to order: keychain\n", + "Enter the quantity for keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 of keychain has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 187, + "id": "43e8311c-45e3-4d4c-82c0-5d90ba7e51b5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 188, + "id": "bb650eeb-f3b9-4d18-88ab-dce09feeefbe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: mug now has 8 left.\n", + "Updated inventory: keychain now has 9 left.\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 189, + "id": "f14c0708-5525-487c-a103-e8bb1b9570c9", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistic = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 190, + "id": "fffa165e-1610-4b4e-90f0-0253c7f018f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total number of ordered products: 3\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "printed_statistic = print_order_statistics(order_statistic)" + ] + }, + { + "cell_type": "code", + "execution_count": 191, + "id": "3c192067-16a2-4bb5-813a-8beae4efb3f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Final updated Inventory:\n", + "t-shirt: 10\n", + "mug: 8\n", + "hat: 10\n", + "book: 10\n", + "keychain: 9\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f5bce281-1a84-4e9f-93df-f932c308cca7", + "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 +686,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.5" } }, "nbformat": 4,