From dabd2cd2cb4576294f8daa189fde92e493869f44 Mon Sep 17 00:00:00 2001 From: Carla Fajula Date: Thu, 23 Jul 2026 14:36:44 +0200 Subject: [PATCH] Solved lab --- lab-python-functions.ipynb | 178 ++++++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 3 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..d175078 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,185 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "ee8ac919-2b54-435e-85c9-76a055433491", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "3c85c119-3070-4091-bf92-160b08e81224", + "metadata": {}, + "outputs": [], + "source": [ + "#1\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product]=quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "e457dccc-92af-4b3d-a30e-bfa8530257ed", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders():\n", + " condition = \"yes\"\n", + " customer_orders = set()\n", + " while condition == \"yes\":\n", + " product = input(f\"Enter the name of a product that the customer wants: \").lower()\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"The product isn't in the list.\")\n", + " \n", + " condition = input(\"Do you want to add another product? \").lower()\n", + " \n", + " #print(\"The order is done.\")\n", + " \n", + " #print(\"The customers order is:\", customer_orders)\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "b6981508-11f8-4e18-9f62-5ec758316f86", + "metadata": {}, + "outputs": [], + "source": [ + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + " #print(\"Updated inventory: \", inventory)\n", + "\n", + " return (inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "17cbd746-fde1-4c74-9851-946025bf3334", + "metadata": {}, + "outputs": [], + "source": [ + "#4\n", + "def calculate_order_statistics (customer_orders, products):\n", + " n_products=len(customer_orders)\n", + " percentage=n_products/len(products) * 100\n", + " return n_products, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "465a9944-a806-44ca-bde9-1a41a9175311", + "metadata": {}, + "outputs": [], + "source": [ + "# 5\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"Total products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of unique products ordered: {order_statistics[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "5ac21ab4-cb8d-4372-ac2d-5f8a3bc50864", + "metadata": {}, + "outputs": [], + "source": [ + "# 6\n", + "def print_updated_inventory(inventory):\n", + " print(\"The updated inventory is: \")\n", + " for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "4b18af5a-d13e-471f-a473-8abe9bb6d14f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt: 45\n", + "Enter the quantity of mug: 87\n", + "Enter the quantity of hat: 9\n", + "Enter the quantity of book: 12\n", + "Enter the quantity of keychain: 45\n", + "Enter the name of a product that the customer wants: t-shirt\n", + "Do you want to add another product? yes\n", + "Enter the name of a product that the customer wants: mug\n", + "Do you want to add another product? yes\n", + "Enter the name of a product that the customer wants: adf\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The product isn't in the list.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? yes\n", + "Enter the name of a product that the customer wants: book\n", + "Do you want to add another product? no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 3\n", + "Percentage of unique products ordered: 60.0%\n", + "The updated inventory is: \n", + "t-shirt: 44\n", + "mug: 86\n", + "hat: 9\n", + "book: 11\n", + "keychain: 45\n" + ] + } + ], + "source": [ + "# 7\n", + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders()\n", + "invent=update_inventory(customer_orders, inventory)\n", + "order_statistics=calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(invent)" + ] } ], "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 +233,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,