From 08582f9068c66718b3807f10b95211569a679252 Mon Sep 17 00:00:00 2001 From: Patricia Viladomiu Date: Sat, 22 Nov 2025 11:50:21 +0100 Subject: [PATCH] Lab Solved --- lab-python-functions.ipynb | 165 ++++++++++++++++++++++++++++++++++++- 1 file changed, 162 insertions(+), 3 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..a4b7716 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,172 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "635fbbb2-c170-4024-be5f-83f1501cd512", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "82e6126b-b349-43c7-967d-81f157346752", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory= {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {products}: \"))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "831a4f7e-7910-452b-b3b4-db4ee642bbdd", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " \n", + " add_more = \"yes\"\n", + " \n", + " while add_more == \"yes\":\n", + " user_order= input(\"Enter a product: \")\n", + "\n", + " if user_order not in products:\n", + " print(\"Invalid product. Try again.\")\n", + " continue\n", + " \n", + " if user_order in customer_orders:\n", + " print(\"Product already chosen. Pick another one.\")\n", + " continue\n", + "\n", + " customer_orders.add(user_order)\n", + "\n", + " add_more = input(\"Add another product? (yes/no): \").lower()\n", + " \n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "88a393ee-fcb3-426b-8e3b-43b90a4607ca", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " update_inventory = {}\n", + " for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is out of stock.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "27a8d5eb-7db2-4e64-81a1-7af14feed136", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products): \n", + " total_ordered = len(customer_orders)\n", + " percentage = (total_ordered / len(products)) * 100\n", + " return total_ordered, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f5834ae1-7fed-4f6b-97c5-d833b7303c3b", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statics):\n", + " total_ordered, percentage = order_statistics\n", + " print(\"Order Statics\")\n", + " print(f\"Total unique products ordered: {total_ordered}\")\n", + " print(f\"Percentage ordered: {percentage}%\") " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "75a65eee-80ea-4c7a-8c18-6910d5595b86", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated inventory\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "76d5d31b-d650-4fdc-b87c-57f75a72a070", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 4\n", + "Enter quantity for ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 4\n", + "Enter quantity for ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 4\n", + "Enter quantity for ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 35\n", + "Enter quantity for ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 12\n", + "Enter a product: keychain\n", + "Add another product? (yes/no): yes\n", + "Enter a product: book\n", + "Add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statics\n", + "Total unique products ordered: 2\n", + "Percentage ordered: 40.0%\n", + "Updated inventory\n", + "t-shirt: 4\n", + "mug: 4\n", + "hat: 4\n", + "book: 34\n", + "keychain: 11\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(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 +220,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,