From dced6ae0e69cc856822497322c0eacc8c61805cf Mon Sep 17 00:00:00 2001 From: Aline Granells Date: Fri, 19 Jun 2026 16:24:13 +0100 Subject: [PATCH] Solved lab --- lab-python-error-handling.ipynb | 139 +++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..4f8180a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,146 @@ "\n", "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bc6cf873", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'f' is not available. Please choose from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "'m' is not available. Please choose from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Total Ordered: 2\n", + "Percentagem of products ordered: 40.0\n", + "t-shirt: 10\n", + "mug: 10\n", + "hat: 9\n", + "book: 9\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory\n", + "\n", + "# Previous exercise:\n", + "'''\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " add_another = \"yes\"\n", + "\n", + " while add_another == \"yes\":\n", + " order = input(\"Please input the name of the product: \").lower().strip()\n", + " customer_orders.add(order)\n", + "\n", + " add_another = input(\"Do you want another product? (yes/no): \").lower().strip()\n", + "\n", + " return customer_orders\n", + "'''\n", + "# Now with the error handling\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " add_another = \"yes\"\n", + "\n", + " while add_another == \"yes\":\n", + " try:\n", + " order = input(\"Please input the name of the product: \").lower().strip()\n", + "\n", + " if order not in products:\n", + " print(f\"'{order}' is not available. Please choose from: {products}\")\n", + " continue\n", + "\n", + " customer_orders.add(order)\n", + "\n", + " while True:\n", + " add_another = input(\"Do you want another product? (yes/no): \").lower().strip()\n", + "\n", + " if add_another in [\"yes\", \"no\"]:\n", + " break\n", + " else:\n", + " print(\"Please type 'yes' or 'no' only.\")\n", + "\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " return {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_order = len(customer_orders)\n", + " rate = (total_order / len(products)) * 100\n", + "\n", + " return total_order, rate\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_order, rate = order_statistics\n", + " \n", + " print(f\"Total Ordered: {total_order}\")\n", + " print(f\"Percentagem of products ordered: {rate}\")\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n", + "\n", + "\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f74e438", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +225,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.5" } }, "nbformat": 4,