From 28e3906b59b2a7c2d6c00d1edf3acc2e52c66974 Mon Sep 17 00:00:00 2001 From: Amanda Joseph Date: Sun, 31 May 2026 17:33:06 +0200 Subject: [PATCH] Lab 3, attempt 1 --- lab-python-functions.ipynb | 234 ++++++++++++++++++++++++++++++++++++- 1 file changed, 231 insertions(+), 3 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..ebeb760 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,241 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "5b905945-5d7f-450e-ac5b-beb2d97e09d1", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product: Glass\n", + "Enter the quantity of the product: 10\n", + "Do you want to add another product? (yes/no) yes\n", + "Enter the name of the product: Mug\n", + "Enter the quantity of the product: 10\n", + "Do you want to add another product? (yes/no) yes\n", + "Enter the name of the product: Teacup\n", + "Enter the quantity of the product: 10\n", + "Do you want to add another product? (yes/no) no\n" + ] + } + ], + "source": [ + "# Task 1: 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.\n", + "\n", + "inventory = {}\n", + "\n", + "def initialize_inventory(products):\n", + " keep_adding = \"yes\"\n", + " while keep_adding == \"yes\":\n", + " products = input(\"Enter the name of the product: \") \n", + " item_quantity = int(input(\"Enter the quantity of the product: \"))\n", + " inventory[products] = item_quantity\n", + " keep_adding = input(\"Do you want to add another product? (yes/no)\")\n", + "\n", + "initialize_inventory(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "71761bd3-66bf-4360-bf7b-3dce5062215c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('Glass', 10), ('Mug', 10), ('Teacup', 10)])" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "105fc9e3-5fd0-41df-b1c7-0ec408d2b2ea", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product you would like to order: Glass\n", + "Enter the quantity of the product: 5\n", + "Do you want to order another product? (yes/no) yes\n", + "Enter the name of the product you would like to order: Mug\n", + "Enter the quantity of the product: 5\n", + "Do you want to order another product? (yes/no) Teacup\n" + ] + }, + { + "data": { + "text/plain": [ + "set()" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Task 2: 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.\n", + "cust_order = {}\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " keep_adding = \"yes\"\n", + " while keep_adding == \"yes\":\n", + " order = input(\"Enter the name of the product you would like to order: \") \n", + " quantity = int(input(\"Enter the quantity of the product: \") )\n", + " cust_order[order] = quantity\n", + " keep_adding = input(\"Do you want to order another product? (yes/no)\")\n", + "\n", + " return customer_orders\n", + "\n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "5ae98937-d85a-4998-b9bd-35cbc074835f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('Glass', 5), ('Mug', 5)])" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cust_order.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "4b5743ea-5712-4995-8d21-f9df9af04dd7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Glass': 5, 'Mug': 5, 'Teacup': 10}" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Task 3: 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.\n", + "\n", + "def update_inventory(cust_order, inventory):\n", + " remaining_inventory = {}\n", + "# For each item in the inventory, look for that item in the customer order and subtract it to get\n", + " for item in inventory:\n", + " remaining_inventory[item] = inventory[item] - cust_order.get(item,0) \n", + " return remaining_inventory\n", + "\n", + "update_inventory(cust_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "3ac0402a-e2ac-4c9d-8dfa-19ef81c291cd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10, 66.66666666666666)" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Task 4: 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 (total products ordered, and percentage of unique \n", + "# products ordered). The function should return these values.\n", + "\n", + "def calculate_order_statistics(cust_order, inventory):\n", + " total_products_ordered = 0\n", + " for quantity in cust_order.values():\n", + " total_products_ordered += quantity\n", + "\n", + " percentage = (len(cust_order) / len(inventory)) * 100\n", + " \n", + " return total_products_ordered, percentage\n", + "\n", + "calculate_order_statistics(cust_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "af8dc424-bd62-409b-a9ff-779dcd49400d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your total products ordered are {'Glass': 5, 'Mug': 5}\n" + ] + } + ], + "source": [ + "# Task 5: 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.\n", + "\n", + "\n", + "def print_order_statistic(order_statistics1, order_statistics2):\n", + " print(\"Your total products ordered are \", order_statistics1)\n", + "\n", + "print_order_statistic(cust_order, inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79a1d6ec-33ce-4469-8b93-75dd42fa02d8", + "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 +289,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,