diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..44d337b --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -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 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..41fac25 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -8,6 +8,14 @@ "# Lab | Functions" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2099bea-19a6-4a48-8266-ddae806d873d", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "0c581062-8967-4d93-b06e-62833222f930", @@ -43,13 +51,145 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b71e656-ba9e-4cc4-aad5-f238123a8828", + "metadata": {}, + "outputs": [], + "source": [ + "#Function 1:\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " number_of_items = int(input(f\"How many {product} are there in the inventory?\"))\n", + " inventory[product] = number_of_items\n", + " return inventory\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products) # creates the inventory from the \"products\" list with quantities from user input\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc1b819a-b149-4bc5-b223-a0bb303153cd", + "metadata": {}, + "outputs": [], + "source": [ + "#Function 2:\n", + "\n", + "def get_customer_orders():\n", + "\n", + " customer_orders = set()\n", + "\n", + " answer = \"yes\"\n", + "\n", + " while answer == \"yes\":\n", + " order = str(input(\"What product does your customer wants to order from the available ones?\").strip().lower())\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Sorry, it's an invalid item\")\n", + "\n", + " answer = input(\"Does the customer want to order anything else? Type yes or no:\").strip().lower()\n", + "\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "838b5586-7112-4711-8420-492701445a6d", + "metadata": {}, + "outputs": [], + "source": [ + "#Function 3:\n", + "\n", + "def update_inventory(customer_orders,inventory):\n", + " updated_inventory = {} #creates a new empty dict \n", + " for product in inventory.keys(): #loops run through the products (keys) in the current inventory \n", + " if product in customer_orders:\n", + " updated_inventory[product] = int(inventory[product] - 1) #only subtracts 1 from customer orders, and updates the values into the new dict\n", + " else:\n", + " updated_inventory[product] = inventory[product] #if the products were not purchased, the values remain the same\n", + " return updated_inventory\n", + "\n", + "updated_inventory = update_inventory(customer_orders, inventory)\n", + "print(updated_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "820b6127-632b-4e0a-9967-e90411924c9d", + "metadata": {}, + "outputs": [], + "source": [ + "#Function 4:\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " percentage_ordered = int((total_products_ordered*100)/(len(products)))\n", + "\n", + " order_statistics = (total_products_ordered, percentage_ordered)\n", + "\n", + " return order_statistics\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c7fd4a9a-c752-4d69-96af-294394f566da", + "metadata": {}, + "outputs": [], + "source": [ + "#Function 5:\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of products ordered: {percentage_ordered}%\")\n", + "\n", + "print_order_statistics(order_statistics)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7fddcb0b-3795-4a3c-a645-cb484e4eea5f", + "metadata": {}, + "outputs": [], + "source": [ + "#Function 6:\n", + "\n", + "def print_updated_inventory(updated_inventory):\n", + " print(\"Updated Inventory:\\n\")\n", + " for product in updated_inventory:\n", + " print(f\"{product}: {updated_inventory[product]}\\n\")\n", + "\n", + "print_updated_inventory(updated_inventory)" + ] } ], "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 +201,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,