From a11a35d14114d50d7688cd8888cf4847f0d8ed76 Mon Sep 17 00:00:00 2001 From: lervindelarosa-gif Date: Wed, 16 Sep 2026 16:52:02 +0200 Subject: [PATCH] W1Lab3 Solved --- lab-python-functions.ipynb | 130 ++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..9560d1b 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,134 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cb7fabfa-8f39-4c56-97f4-ee53180c1e8f", + "metadata": {}, + "outputs": [], + "source": [ + "# Catalog list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0a11f91f-4fcf-4d37-9397-ef8a84a95bcd", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named initialize_inventory\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for item in products:\n", + " qty = int(input(f\"Enter quantity for {item}: \"))\n", + " inventory[item] = qty\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea53a544-dda7-47cc-a187-62734e6d35aa", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named get_customer_orders\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " add_another = \"yes\"\n", + " \n", + " while add_another.lower() == \"yes\":\n", + " product = input(\"Enter the name of a product: \")\n", + " customer_orders.add(product)\n", + " add_another = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4b94c93-9188-4e19-b888-38137f343129", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named update_inventory\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c11bd6d7-e472-4ffe-bde9-b86693dbc0c4", + "metadata": {}, + "outputs": [], + "source": [ + "# calculate_order_statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_ordered = len(customer_orders)\n", + " percentage_ordered = (len(customer_orders) / len(products)) * 100\n", + " order_status = (total_ordered, percentage_ordered)\n", + " return order_status" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a027df1-55b3-4714-b110-61d5b56959e6", + "metadata": {}, + "outputs": [], + "source": [ + "# print_order_statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of Products Ordered: {order_statistics[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5a70e3a6-0816-4112-9826-3d89003d8e60", + "metadata": {}, + "outputs": [], + "source": [ + "# print_updated_inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab25a992-6cfc-46b9-9d4e-dd60c0ebbdcb", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_stats)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { @@ -61,7 +189,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,