From bc63365d97a4a380263f2b17cb24f5e60f79fb87 Mon Sep 17 00:00:00 2001 From: Melissa Sanchez Date: Wed, 17 Jun 2026 17:40:09 +0200 Subject: [PATCH] completed functions lab --- .../lab-python-functions-checkpoint.ipynb | 285 ++++++++++++++++++ lab-python-functions.ipynb | 218 +++++++++++++- 2 files changed, 502 insertions(+), 1 deletion(-) 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..dc4fe34 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,285 @@ +{ + "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": "markdown", + "id": "5ab31e78-b58d-486b-8359-32eaa22b2463", + "metadata": {}, + "source": [ + "## Functions" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2b746a12-1c40-4eed-8b66-f8512ce15fcc", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " quantity = int(input(\"Please add the number of items for \" + product))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "68883983-edff-4fde-9597-c0c3b8b0de42", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add the number of items for t-shirt 10\n", + "Please add the number of items for mug 8\n", + "Please add the number of items for hat 5\n", + "Please add the number of items for book 12\n", + "Please add the number of items for keychain 4\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "64ba8010-09c4-4b37-af52-6461d89fafd2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 8, 'hat': 5, 'book': 12, 'keychain': 4}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ea3e3161-c25e-4f83-92bd-314d33de275d", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " order_again = \"yes\"\n", + " \n", + " while order_again == \"yes\":\n", + " product = input(\"Please add the product you would like to order:\").lower().strip()\n", + " customer_orders.add(product)\n", + " order_again = input(\"Would you like to continue ordering? Please answer with yes or no.\").lower().strip()\n", + "\n", + " print (\"Thank you for your order!\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a79abe87-66a2-440c-8e04-2c0782aca7ad", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add the product you would like to order: mug\n", + "Would you like to continue ordering? Please answer with yes or no. yes\n", + "Please add the product you would like to order: t-shirt\n", + "Would you like to continue ordering? Please answer with yes or no. yes\n", + "Please add the product you would like to order: book\n", + "Would you like to continue ordering? Please answer with yes or no. yes\n", + "Please add the product you would like to order: keychain\n", + "Would you like to continue ordering? Please answer with yes or no. no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for your order!\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8802498b-a518-4c08-bcba-b7d69ebd1bc3", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product]-=1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "972e04a2-c2af-4356-8112-54bf7edce601", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "787d4744-8224-412a-8986-59b6bca348b3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 9, 'mug': 7, 'hat': 5, 'book': 11, 'keychain': 3}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c008ba87-10d3-4061-a700-3fcc23f6d058", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_products_ordered = (total_products_ordered / len(products))*100\n", + " return total_products_ordered, percentage_unique_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3958a306-aef9-49ca-b40b-d7af24218c10", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "5d79e8d9-e994-437f-9125-8ea7633c407e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 80.0)" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "608d0bf9-7ae1-4f0e-beb3-110b30b067b3", + "metadata": {}, + "outputs": [], + "source": [ + "##to be continued" + ] + } + ], + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..dc4fe34 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,222 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "id": "5ab31e78-b58d-486b-8359-32eaa22b2463", + "metadata": {}, + "source": [ + "## Functions" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2b746a12-1c40-4eed-8b66-f8512ce15fcc", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " quantity = int(input(\"Please add the number of items for \" + product))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "68883983-edff-4fde-9597-c0c3b8b0de42", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add the number of items for t-shirt 10\n", + "Please add the number of items for mug 8\n", + "Please add the number of items for hat 5\n", + "Please add the number of items for book 12\n", + "Please add the number of items for keychain 4\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "64ba8010-09c4-4b37-af52-6461d89fafd2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 8, 'hat': 5, 'book': 12, 'keychain': 4}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ea3e3161-c25e-4f83-92bd-314d33de275d", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " order_again = \"yes\"\n", + " \n", + " while order_again == \"yes\":\n", + " product = input(\"Please add the product you would like to order:\").lower().strip()\n", + " customer_orders.add(product)\n", + " order_again = input(\"Would you like to continue ordering? Please answer with yes or no.\").lower().strip()\n", + "\n", + " print (\"Thank you for your order!\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a79abe87-66a2-440c-8e04-2c0782aca7ad", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add the product you would like to order: mug\n", + "Would you like to continue ordering? Please answer with yes or no. yes\n", + "Please add the product you would like to order: t-shirt\n", + "Would you like to continue ordering? Please answer with yes or no. yes\n", + "Please add the product you would like to order: book\n", + "Would you like to continue ordering? Please answer with yes or no. yes\n", + "Please add the product you would like to order: keychain\n", + "Would you like to continue ordering? Please answer with yes or no. no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for your order!\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8802498b-a518-4c08-bcba-b7d69ebd1bc3", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product]-=1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "972e04a2-c2af-4356-8112-54bf7edce601", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "787d4744-8224-412a-8986-59b6bca348b3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 9, 'mug': 7, 'hat': 5, 'book': 11, 'keychain': 3}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c008ba87-10d3-4061-a700-3fcc23f6d058", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_products_ordered = (total_products_ordered / len(products))*100\n", + " return total_products_ordered, percentage_unique_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3958a306-aef9-49ca-b40b-d7af24218c10", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "5d79e8d9-e994-437f-9125-8ea7633c407e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 80.0)" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "608d0bf9-7ae1-4f0e-beb3-110b30b067b3", + "metadata": {}, + "outputs": [], + "source": [ + "##to be continued" + ] } ], "metadata": { @@ -61,7 +277,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,