From 58a7d2f448091c627f307d613b0c19309b12e311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Thu, 16 Apr 2026 17:58:31 +0200 Subject: [PATCH 1/4] test error handling --- lab-python-error-handling.ipynb | 137 +++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..1128971 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,144 @@ "\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": null, + "id": "2ce25673", + "metadata": {}, + "outputs": [], + "source": [ + "def manage_orders():\n", + " def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity of {product}: \").strip())\n", + " if quantity >= 0:\n", + " quantity = int(quantity)\n", + " inventory[product] = quantity\n", + " break\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", + "\n", + " print(\"------Availability of products------\")\n", + " for item,value in inventory.items():\n", + " print(item,\":\",value) \n", + " \n", + " return inventory\n", + "\n", + " def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " try:\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + "\n", + " if order not in inventory:\n", + " raise KeyError\n", + " if inventory[order] <= 0:\n", + " raise ValueError\n", + " \n", + " customer_orders.add(order)\n", + " inventory[order] -= 1\n", + " except KeyError:\n", + " print(f'Product {order} not found')\n", + " except ValueError:\n", + " print(f'Sorry, {order} is out of stock')\n", + " \n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", + " while validation not in ('yes','no'):\n", + " print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\", \"\\n\")\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower() \n", + "\n", + " if validation == \"no\":\n", + " break \n", + "\n", + " print(\"------Customer´s order------\")\n", + " print (f\"The customer has ordered {customer_orders}\", \"\\n\")\n", + "\n", + " return customer_orders\n", + " \n", + " def calculate_order_statistics(customer_orders,products):\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = round((total_products_ordered/len(products))*100,1)\n", + "\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + " def print_order_statistics(order_statistics):\n", + "\n", + " total_products_ordered, percentage_of_products_ordered = order_statistics\n", + " print(\"------Order Statistics------\")\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of products ordered: {percentage_of_products_ordered}%\",\"\\n\")\n", + "\n", + " def print_updated_inventory(inventory):\n", + " print(\"------Updated inventory------\")\n", + " for key,value in inventory.items():\n", + " print(f\"{key}: {value}\")\n", + " print()\n", + "\n", + " def total_price(customer_orders):\n", + "\n", + " prices_list = []\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " user_input = float(input(f'Enter the price of one {product}').replace(',','.'))\n", + " if user_input >= 0:\n", + " prices_list.append(user_input)\n", + " break\n", + " else:\n", + " print('Price cannot be negative. Please enter a valid price')\n", + " except ValueError:\n", + " print('Invalid input. Price must be a number.')\n", + "\n", + " print('------Receipt------')\n", + " for price,item in zip(prices_list,customer_orders):\n", + " print(f'The price of one {item} is: {price}$ ') \n", + "\n", + " sum_prices = round(sum(prices_list),1)\n", + " \n", + " return sum_prices\n", + "\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " inventory = initialize_inventory(products) \n", + " customer_orders = get_customer_orders(inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders,products)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(inventory)\n", + " total = total_price(customer_orders)\n", + " print(f'the total price is: {total}$')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d492c33d", + "metadata": {}, + "outputs": [], + "source": [ + "manage_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2fa4bb28", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +223,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4, From 446353bd8ad3f41feb843b4c2332af082532920c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Thu, 16 Apr 2026 18:36:36 +0200 Subject: [PATCH 2/4] improve test --- lab-python-error-handling.ipynb | 59 ++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 1128971..e495962 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "2ce25673", "metadata": {}, "outputs": [], @@ -83,23 +83,19 @@ "def manage_orders():\n", " def initialize_inventory(products):\n", " inventory = {}\n", + " products = [item.lower().strip() for item in products]\n", " for product in products:\n", " while True:\n", " try:\n", " quantity = int(input(f\"Enter quantity of {product}: \").strip())\n", " if quantity >= 0:\n", - " quantity = int(quantity)\n", " inventory[product] = quantity\n", " break\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", - "\n", - " print(\"------Availability of products------\")\n", - " for item,value in inventory.items():\n", - " print(item,\":\",value) \n", - " \n", + " \n", " return inventory\n", "\n", " def get_customer_orders(inventory):\n", @@ -154,7 +150,7 @@ " print(f\"{key}: {value}\")\n", " print()\n", "\n", - " def total_price(customer_orders):\n", + " def calculate_total_price(customer_orders):\n", "\n", " prices_list = []\n", " for product in customer_orders:\n", @@ -177,23 +173,62 @@ " \n", " return sum_prices\n", "\n", - " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " products = [\"T-shirt\", \"MUg\", \"hat\", \"bOok\", \"keychain\"]\n", "\n", " inventory = initialize_inventory(products) \n", + " print(\"------Availability of products------\")\n", + " for item,value in inventory.items():\n", + " print(item,\":\",value) \n", " customer_orders = get_customer_orders(inventory)\n", " order_statistics = calculate_order_statistics(customer_orders,products)\n", " print_order_statistics(order_statistics)\n", " print_updated_inventory(inventory)\n", - " total = total_price(customer_orders)\n", + " total = calculate_total_price(customer_orders)\n", " print(f'the total price is: {total}$')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "d492c33d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity\n", + "------Availability of products------\n", + "t-shirt : 1\n", + "mug : 1\n", + "hat : 1\n", + "book : 1\n", + "keychain : 1\n", + "Product 1 not found\n", + "'mug' is not a correct option. Please select 'yes' or 'no'. \n", + "\n", + "Product yes not found\n", + "'book' is not a correct option. Please select 'yes' or 'no'. \n", + "\n", + "------Customer´s order------\n", + "The customer has ordered set() \n", + "\n", + "------Order Statistics------\n", + "Total products ordered: 0\n", + "Percentage of products ordered: 0.0% \n", + "\n", + "------Updated inventory------\n", + "t-shirt: 1\n", + "mug: 1\n", + "hat: 1\n", + "book: 1\n", + "keychain: 1\n", + "\n", + "------Receipt------\n", + "the total price is: 0$\n" + ] + } + ], "source": [ "manage_orders()" ] From 76f54aff3abd085c94a525800b3dcfcc16387bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Thu, 16 Apr 2026 18:48:36 +0200 Subject: [PATCH 3/4] test --- lab-python-error-handling.ipynb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index e495962..baa0d53 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -154,6 +154,7 @@ "\n", " prices_list = []\n", " for product in customer_orders:\n", + "\n", " while True:\n", " try:\n", " user_input = float(input(f'Enter the price of one {product}').replace(',','.'))\n", @@ -166,7 +167,7 @@ " print('Invalid input. Price must be a number.')\n", "\n", " print('------Receipt------')\n", - " for price,item in zip(prices_list,customer_orders):\n", + " for price,item in zip(prices_list,list(customer_orders)):\n", " print(f'The price of one {item} is: {price}$ ') \n", "\n", " sum_prices = round(sum(prices_list),1)\n", @@ -197,35 +198,30 @@ "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a valid quantity\n", "------Availability of products------\n", "t-shirt : 1\n", "mug : 1\n", "hat : 1\n", "book : 1\n", "keychain : 1\n", - "Product 1 not found\n", - "'mug' is not a correct option. Please select 'yes' or 'no'. \n", - "\n", - "Product yes not found\n", - "'book' is not a correct option. Please select 'yes' or 'no'. \n", - "\n", "------Customer´s order------\n", - "The customer has ordered set() \n", + "The customer has ordered {'mug', 'book'} \n", "\n", "------Order Statistics------\n", - "Total products ordered: 0\n", - "Percentage of products ordered: 0.0% \n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.0% \n", "\n", "------Updated inventory------\n", "t-shirt: 1\n", - "mug: 1\n", + "mug: 0\n", "hat: 1\n", - "book: 1\n", + "book: 0\n", "keychain: 1\n", "\n", "------Receipt------\n", - "the total price is: 0$\n" + "The price of one mug is: 34.5$ \n", + "The price of one book is: 23.8$ \n", + "the total price is: 58.3$\n" ] } ], From c67969a62ed3c2d0950f73da2a0e5c446c52e056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Sat, 18 Apr 2026 00:03:54 +0200 Subject: [PATCH 4/4] update changes --- lab-python-error-handling.ipynb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index baa0d53..83ad090 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "2ce25673", "metadata": {}, "outputs": [], @@ -164,7 +164,7 @@ " else:\n", " print('Price cannot be negative. Please enter a valid price')\n", " except ValueError:\n", - " print('Invalid input. Price must be a number.')\n", + " print('Invalid input. Price has be a number.')\n", "\n", " print('------Receipt------')\n", " for price,item in zip(prices_list,list(customer_orders)):\n", @@ -190,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "id": "d492c33d", "metadata": {}, "outputs": [ @@ -204,24 +204,29 @@ "hat : 1\n", "book : 1\n", "keychain : 1\n", + "Product mu not found\n", + "Sorry, mug is out of stock\n", + "'45' is not a correct option. Please select 'yes' or 'no'. \n", + "\n", "------Customer´s order------\n", - "The customer has ordered {'mug', 'book'} \n", + "The customer has ordered {'mug'} \n", "\n", "------Order Statistics------\n", - "Total products ordered: 2\n", - "Percentage of products ordered: 40.0% \n", + "Total products ordered: 1\n", + "Percentage of products ordered: 20.0% \n", "\n", "------Updated inventory------\n", "t-shirt: 1\n", "mug: 0\n", "hat: 1\n", - "book: 0\n", + "book: 1\n", "keychain: 1\n", "\n", + "Invalid input. Price must be a number.\n", + "Price cannot be negative. Please enter a valid price\n", "------Receipt------\n", "The price of one mug is: 34.5$ \n", - "The price of one book is: 23.8$ \n", - "the total price is: 58.3$\n" + "the total price is: 34.5$\n" ] } ],