diff --git a/lab-functions_elbgross.ipynb b/lab-functions_elbgross.ipynb new file mode 100644 index 0000000..98b8300 --- /dev/null +++ b/lab-functions_elbgross.ipynb @@ -0,0 +1,242 @@ +{ + "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": 21, + "id": "61865a0e", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "could not convert string to float: ''", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 17\u001b[39m\n\u001b[32m 10\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m inventory\n\u001b[32m 12\u001b[39m \u001b[38;5;66;03m# You cannot use return print(inventory) bc the function won´t allow you to use the inventory in other lines of the code \u001b[39;00m\n\u001b[32m 13\u001b[39m \u001b[38;5;66;03m# is better to print it out of the function always\u001b[39;00m\n\u001b[32m 14\u001b[39m \n\u001b[32m 15\u001b[39m \u001b[38;5;66;03m#We always call the function after we run it to save it and python can recognise it\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m17\u001b[39m inventory=\u001b[43minitialize_inventory\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproducts\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 19\u001b[39m \u001b[38;5;66;03m#We define inventory in our code after we define the function so python can recognise it\u001b[39;00m\n\u001b[32m 21\u001b[39m \u001b[38;5;28mprint\u001b[39m(inventory) \n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 7\u001b[39m, in \u001b[36minitialize_inventory\u001b[39m\u001b[34m(products)\u001b[39m\n\u001b[32m 5\u001b[39m inventory={}\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m quantity=\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43mf\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mPlease enter the quantity for \u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mproduct\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m#here we are defining that product is the key on the dictionary and quantity is the value\u001b[39;00m\n\u001b[32m 9\u001b[39m inventory[product]=quantity\n", + "\u001b[31mValueError\u001b[39m: could not convert string to float: ''" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + " \n", + "\n", + "def initialize_inventory(products):\n", + " inventory={}\n", + " for product in products:\n", + " quantity=float(input(f\"Please enter the quantity for {product}\"))\n", + " #here we are defining that product is the key on the dictionary and quantity is the value\n", + " inventory[product]=quantity\n", + " return inventory\n", + "\n", + "# You cannot use return print(inventory) bc the function won´t allow you to use the inventory in other lines of the code \n", + "# is better to print it out of the function always\n", + "\n", + "#We always call the function after we run it to save it and python can recognise it\n", + "\n", + "inventory=initialize_inventory(products)\n", + "\n", + "#We define inventory in our code after we define the function so python can recognise it\n", + "\n", + "print(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cdcd4bb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug'}\n" + ] + } + ], + "source": [ + "def get_customer_orders(products):\n", + " customers_orders=set()\n", + " another=\"yes\"\n", + " another.lower()\n", + " while another==\"yes\":\n", + " order_product=input(\"Please choose an item from the product list\")\n", + " if order_product not in products:\n", + " print(\"This product is not available\")\n", + " continue\n", + " if order_product in customers_orders:\n", + " print(\"You already selected this product\")\n", + " continue\n", + " customers_orders.add(order_product)\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " return customers_orders\n", + "\n", + "customers_orders=get_customer_orders(products)\n", + "print(customers_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ecef3c4b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5.0, 'mug': 4.0, 'hat': 2.0, 'book': 5.0, 'keychain': 5.0}\n" + ] + } + ], + "source": [ + "def update_inventory(customers_orders,inventory):\n", + " # This function remove all the products that the customer ordered from the original inventory\n", + " updated_inventory={}\n", + " # For every product that client ordered we remove one from the inventory:\n", + " for products in customers_orders:\n", + " if products in inventory:\n", + " inventory[products]-=1\n", + " return inventory\n", + "\n", + "# We have to return inventory and it will be updated with the condictions that we put in our function\n", + "\n", + "updated_inventory=update_inventory(customers_orders, inventory)\n", + "\n", + "print(updated_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c7550d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 40.0)\n" + ] + } + ], + "source": [ + "def calculate_order_statistics(customers_orders,products):\n", + " total_products_ordered=len(customers_orders)\n", + " \n", + " percentage_products_ordered=(len(customers_orders)/len(products))*100\n", + " \n", + " return (total_products_ordered,percentage_products_ordered)\n", + "\n", + "# it can return more than two things\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0325d331", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_ordered, unique_products, percentage_unique = order_statistics\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " print(f\"Percentage of unique products: {percentage_unique:.2f}%\")\n", + " print(\"order_statistics\")\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "f801c30a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your inventory after the order is: {'t-shirt': 5.0, 'mug': 4.0, 'hat': 2.0, 'book': 5.0, 'keychain': 5.0}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(f\"Your inventory after the order is: {updated_inventory}\")\n", + "\n", + "print_updated_inventory(inventory)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.13.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..98b8300 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,184 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "61865a0e", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "could not convert string to float: ''", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 17\u001b[39m\n\u001b[32m 10\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m inventory\n\u001b[32m 12\u001b[39m \u001b[38;5;66;03m# You cannot use return print(inventory) bc the function won´t allow you to use the inventory in other lines of the code \u001b[39;00m\n\u001b[32m 13\u001b[39m \u001b[38;5;66;03m# is better to print it out of the function always\u001b[39;00m\n\u001b[32m 14\u001b[39m \n\u001b[32m 15\u001b[39m \u001b[38;5;66;03m#We always call the function after we run it to save it and python can recognise it\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m17\u001b[39m inventory=\u001b[43minitialize_inventory\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproducts\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 19\u001b[39m \u001b[38;5;66;03m#We define inventory in our code after we define the function so python can recognise it\u001b[39;00m\n\u001b[32m 21\u001b[39m \u001b[38;5;28mprint\u001b[39m(inventory) \n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 7\u001b[39m, in \u001b[36minitialize_inventory\u001b[39m\u001b[34m(products)\u001b[39m\n\u001b[32m 5\u001b[39m inventory={}\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m quantity=\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43mf\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mPlease enter the quantity for \u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mproduct\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m#here we are defining that product is the key on the dictionary and quantity is the value\u001b[39;00m\n\u001b[32m 9\u001b[39m inventory[product]=quantity\n", + "\u001b[31mValueError\u001b[39m: could not convert string to float: ''" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + " \n", + "\n", + "def initialize_inventory(products):\n", + " inventory={}\n", + " for product in products:\n", + " quantity=float(input(f\"Please enter the quantity for {product}\"))\n", + " #here we are defining that product is the key on the dictionary and quantity is the value\n", + " inventory[product]=quantity\n", + " return inventory\n", + "\n", + "# You cannot use return print(inventory) bc the function won´t allow you to use the inventory in other lines of the code \n", + "# is better to print it out of the function always\n", + "\n", + "#We always call the function after we run it to save it and python can recognise it\n", + "\n", + "inventory=initialize_inventory(products)\n", + "\n", + "#We define inventory in our code after we define the function so python can recognise it\n", + "\n", + "print(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cdcd4bb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug'}\n" + ] + } + ], + "source": [ + "def get_customer_orders(products):\n", + " customers_orders=set()\n", + " another=\"yes\"\n", + " another.lower()\n", + " while another==\"yes\":\n", + " order_product=input(\"Please choose an item from the product list\")\n", + " if order_product not in products:\n", + " print(\"This product is not available\")\n", + " continue\n", + " if order_product in customers_orders:\n", + " print(\"You already selected this product\")\n", + " continue\n", + " customers_orders.add(order_product)\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " return customers_orders\n", + "\n", + "customers_orders=get_customer_orders(products)\n", + "print(customers_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ecef3c4b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5.0, 'mug': 4.0, 'hat': 2.0, 'book': 5.0, 'keychain': 5.0}\n" + ] + } + ], + "source": [ + "def update_inventory(customers_orders,inventory):\n", + " # This function remove all the products that the customer ordered from the original inventory\n", + " updated_inventory={}\n", + " # For every product that client ordered we remove one from the inventory:\n", + " for products in customers_orders:\n", + " if products in inventory:\n", + " inventory[products]-=1\n", + " return inventory\n", + "\n", + "# We have to return inventory and it will be updated with the condictions that we put in our function\n", + "\n", + "updated_inventory=update_inventory(customers_orders, inventory)\n", + "\n", + "print(updated_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c7550d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 40.0)\n" + ] + } + ], + "source": [ + "def calculate_order_statistics(customers_orders,products):\n", + " total_products_ordered=len(customers_orders)\n", + " \n", + " percentage_products_ordered=(len(customers_orders)/len(products))*100\n", + " \n", + " return (total_products_ordered,percentage_products_ordered)\n", + "\n", + "# it can return more than two things\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0325d331", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_ordered, unique_products, percentage_unique = order_statistics\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " print(f\"Percentage of unique products: {percentage_unique:.2f}%\")\n", + " print(\"order_statistics\")\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "f801c30a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your inventory after the order is: {'t-shirt': 5.0, 'mug': 4.0, 'hat': 2.0, 'book': 5.0, 'keychain': 5.0}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(f\"Your inventory after the order is: {updated_inventory}\")\n", + "\n", + "print_updated_inventory(inventory)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +234,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.6" } }, "nbformat": 4,