From 64c34f7ccf5609402149000cb24c9a47634055a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tebas=20Mart=C3=ADnez?= <203688711+TebasMartinez@users.noreply.github.com> Date: Sat, 12 Jul 2025 23:33:19 +0200 Subject: [PATCH 1/3] solve lab --- ...lab-python-error-handling-checkpoint.ipynb | 305 ++++++++++++++++++ lab-python-error-handling.ipynb | 241 +++++++++++++- 2 files changed, 541 insertions(+), 5 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb new file mode 100644 index 0000000..55ac662 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,305 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to identify, handle and recover from potential errors in Python code using try-except blocks." + ] + }, + { + "cell_type": "markdown", + "id": "e253e768-aed8-4791-a800-87add1204afa", + "metadata": {}, + "source": [ + "## Challenge \n", + "\n", + "Paste here your lab *functions* solutions. Apply error handling techniques to each function using try-except blocks. " + ] + }, + { + "cell_type": "markdown", + "id": "9180ff86-c3fe-4152-a609-081a287fa1af", + "metadata": {}, + "source": [ + "The try-except block in Python is designed to handle exceptions and provide a fallback mechanism when code encounters errors. By enclosing the code that could potentially throw errors in a try block, followed by specific or general exception handling in the except block, we can gracefully recover from errors and continue program execution.\n", + "\n", + "However, there may be cases where an input may not produce an immediate error, but still needs to be addressed. In such situations, it can be useful to explicitly raise an error using the \"raise\" keyword, either to draw attention to the issue or handle it elsewhere in the program.\n", + "\n", + "Modify the code to handle possible errors in Python, it is recommended to use `try-except-else-finally` blocks, incorporate the `raise` keyword where necessary, and print meaningful error messages to alert users of any issues that may occur during program execution.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", + "metadata": {}, + "outputs": [], + "source": [ + "def main():\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders(inventory)\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "1933703b", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " qty = input(f\"Please provide number of {product} available: \")\n", + " if not qty.isnumeric():\n", + " raise ValueError(\"Invalid input. Please enter a non negative number.\") \n", + " inventory[product] = int(qty)\n", + " valid_input = True\n", + " except ValueError as v:\n", + " print(v)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "f81c9580", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " qty = int((input(\"Enter the number of customer orders: \")))\n", + " if qty > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of customer orders cannot be zero or negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number of customer orders\")\n", + " customer_orders = set()\n", + " for i in range(qty):\n", + " valid_input = False\n", + " while not valid_input:\n", + " order = input(\"Enter the name of a product that a customer wants to order:\")\n", + " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", + " print(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", + " elif inventory[order] <= 0:\n", + " print(f\"The product is not in stock! Our current stock is: {inventory}\")\n", + " else:\n", + " customer_orders.add(order)\n", + " valid_input = True\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "d30e75c9", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory.keys():\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "741c83b5", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " perc = (total_products_ordered / len(products)) * 100\n", + " order_statistics = [total_products_ordered, int(perc)]\n", + " return order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "b7e0e106", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(f\"The total number of products ordered is {order_statistics[0]}, and the percentage of unique products ordered is {order_statistics[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "f109be01", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(f\"The updated inventory is: {inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "99d37db8-caec-43fc-b34e-fd9ecf57d78d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a non negative number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a non negative number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: 7\n", + "Please provide number of mug available: 2\n", + "Please provide number of hat available: 1\n", + "Please provide number of book available: 34\n", + "Please provide number of keychain available: 5\n", + "Enter the number of customer orders: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a number of customer orders\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of customer orders cannot be zero or negative. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of customer orders cannot be zero or negative. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 3\n", + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: laptop\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The product is not in the inventory. Products available are: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of products ordered is 3, and the percentage of unique products ordered is 60%\n", + "The updated inventory is: {'t-shirt': 7, 'mug': 1, 'hat': 0, 'book': 33, 'keychain': 5}\n" + ] + } + ], + "source": [ + "main()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..55ac662 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,20 +41,251 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def main():\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders(inventory)\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "1933703b", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " qty = input(f\"Please provide number of {product} available: \")\n", + " if not qty.isnumeric():\n", + " raise ValueError(\"Invalid input. Please enter a non negative number.\") \n", + " inventory[product] = int(qty)\n", + " valid_input = True\n", + " except ValueError as v:\n", + " print(v)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "f81c9580", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " qty = int((input(\"Enter the number of customer orders: \")))\n", + " if qty > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of customer orders cannot be zero or negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number of customer orders\")\n", + " customer_orders = set()\n", + " for i in range(qty):\n", + " valid_input = False\n", + " while not valid_input:\n", + " order = input(\"Enter the name of a product that a customer wants to order:\")\n", + " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", + " print(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", + " elif inventory[order] <= 0:\n", + " print(f\"The product is not in stock! Our current stock is: {inventory}\")\n", + " else:\n", + " customer_orders.add(order)\n", + " valid_input = True\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "d30e75c9", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory.keys():\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "741c83b5", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " perc = (total_products_ordered / len(products)) * 100\n", + " order_statistics = [total_products_ordered, int(perc)]\n", + " return order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "b7e0e106", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(f\"The total number of products ordered is {order_statistics[0]}, and the percentage of unique products ordered is {order_statistics[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "f109be01", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(f\"The updated inventory is: {inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "99d37db8-caec-43fc-b34e-fd9ecf57d78d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a non negative number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a non negative number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: 7\n", + "Please provide number of mug available: 2\n", + "Please provide number of hat available: 1\n", + "Please provide number of book available: 34\n", + "Please provide number of keychain available: 5\n", + "Enter the number of customer orders: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a number of customer orders\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of customer orders cannot be zero or negative. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of customer orders cannot be zero or negative. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 3\n", + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: laptop\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The product is not in the inventory. Products available are: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of products ordered is 3, and the percentage of unique products ordered is 60%\n", + "The updated inventory is: {'t-shirt': 7, 'mug': 1, 'hat': 0, 'book': 33, 'keychain': 5}\n" + ] + } + ], + "source": [ + "main()" ] } ], "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": { @@ -66,7 +297,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From 2f1a59be8f65764249bcbec721f0514074cdeb85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tebas=20Mart=C3=ADnez?= <203688711+TebasMartinez@users.noreply.github.com> Date: Sun, 13 Jul 2025 00:25:48 +0200 Subject: [PATCH 2/3] add more error handling cases following lab feedback --- ...lab-python-error-handling-checkpoint.ipynb | 163 ++++++++++++++---- lab-python-error-handling.ipynb | 163 ++++++++++++++---- 2 files changed, 262 insertions(+), 64 deletions(-) diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb index 55ac662..2f0aec0 100644 --- a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 30, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 31, "id": "1933703b", "metadata": {}, "outputs": [], @@ -72,16 +72,19 @@ " qty = input(f\"Please provide number of {product} available: \")\n", " if not qty.isnumeric():\n", " raise ValueError(\"Invalid input. Please enter a non negative number.\") \n", - " inventory[product] = int(qty)\n", - " valid_input = True\n", " except ValueError as v:\n", " print(v)\n", + " else:\n", + " inventory[product] = int(qty)\n", + " valid_input = True\n", + " finally:\n", + " print(f\"Currently, these are the products in the inventory: {inventory}\")\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 32, "id": "f81c9580", "metadata": {}, "outputs": [], @@ -101,48 +104,61 @@ " for i in range(qty):\n", " valid_input = False\n", " while not valid_input:\n", - " order = input(\"Enter the name of a product that a customer wants to order:\")\n", - " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", - " print(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", - " elif inventory[order] <= 0:\n", - " print(f\"The product is not in stock! Our current stock is: {inventory}\")\n", + " try:\n", + " order = input(\"Enter the name of a product that a customer wants to order:\")\n", + " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", + " raise TypeError(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", + " elif inventory[order] <= 0:\n", + " raise ValueError(f\"The product is not in stock! Our current stock is: {inventory}\")\n", + " except TypeError as t:\n", + " print(t)\n", + " except ValueError as v:\n", + " print(v)\n", " else:\n", - " customer_orders.add(order)\n", - " valid_input = True\n", + " customer_orders.add(order.lower())\n", + " valid_input = True\n", " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 33, "id": "d30e75c9", "metadata": {}, "outputs": [], "source": [ "def update_inventory(customer_orders, inventory):\n", " for product in customer_orders:\n", - " if product in inventory.keys():\n", - " inventory[product] -= 1\n", + " try:\n", + " inventory[product]\n", + " except KeyError as k:\n", + " print(\"At least one of the products in the customer order is no in the inventory\")\n", + " else:\n", + " for product in inventory.keys():\n", + " inventory[product] -= 1\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 34, "id": "741c83b5", "metadata": {}, "outputs": [], "source": [ "def calculate_order_statistics(customer_orders, products):\n", " total_products_ordered = len(customer_orders)\n", - " perc = (total_products_ordered / len(products)) * 100\n", + " try:\n", + " perc = (total_products_ordered / len(products)) * 100\n", + " except ZeroDivisionError:\n", + " print(\"Please add products to the list of products.\")\n", " order_statistics = [total_products_ordered, int(perc)]\n", " return order_statistics" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 35, "id": "b7e0e106", "metadata": {}, "outputs": [], @@ -153,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 36, "id": "f109be01", "metadata": {}, "outputs": [], @@ -164,9 +180,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 37, "id": "99d37db8-caec-43fc-b34e-fd9ecf57d78d", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdin", @@ -179,32 +197,99 @@ "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n" + "Invalid input. Please enter a non negative number.\n", + "Currently, these are the products in the inventory: {}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: 02\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of mug available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a non negative number.\n", + "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of mug available: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of hat available: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of t-shirt available: -3\n" + "Please provide number of book available: 9\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n" + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of keychain available: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of t-shirt available: 7\n", - "Please provide number of mug available: 2\n", - "Please provide number of hat available: 1\n", - "Please provide number of book available: 34\n", - "Please provide number of keychain available: 5\n", "Enter the number of customer orders: no\n" ] }, @@ -248,7 +333,20 @@ "output_type": "stream", "text": [ "Enter the number of customer orders: 3\n", - "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The product is not in stock! Our current stock is: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ "Enter the name of a product that a customer wants to order: laptop\n" ] }, @@ -263,7 +361,8 @@ "name": "stdin", "output_type": "stream", "text": [ - "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: t-shirt\n", + "Enter the name of a product that a customer wants to order: hat\n", "Enter the name of a product that a customer wants to order: book\n" ] }, @@ -272,7 +371,7 @@ "output_type": "stream", "text": [ "The total number of products ordered is 3, and the percentage of unique products ordered is 60%\n", - "The updated inventory is: {'t-shirt': 7, 'mug': 1, 'hat': 0, 'book': 33, 'keychain': 5}\n" + "The updated inventory is: {'t-shirt': -1, 'mug': -3, 'hat': 31, 'book': 6, 'keychain': 2}\n" ] } ], diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 55ac662..2f0aec0 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 30, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 31, "id": "1933703b", "metadata": {}, "outputs": [], @@ -72,16 +72,19 @@ " qty = input(f\"Please provide number of {product} available: \")\n", " if not qty.isnumeric():\n", " raise ValueError(\"Invalid input. Please enter a non negative number.\") \n", - " inventory[product] = int(qty)\n", - " valid_input = True\n", " except ValueError as v:\n", " print(v)\n", + " else:\n", + " inventory[product] = int(qty)\n", + " valid_input = True\n", + " finally:\n", + " print(f\"Currently, these are the products in the inventory: {inventory}\")\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 32, "id": "f81c9580", "metadata": {}, "outputs": [], @@ -101,48 +104,61 @@ " for i in range(qty):\n", " valid_input = False\n", " while not valid_input:\n", - " order = input(\"Enter the name of a product that a customer wants to order:\")\n", - " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", - " print(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", - " elif inventory[order] <= 0:\n", - " print(f\"The product is not in stock! Our current stock is: {inventory}\")\n", + " try:\n", + " order = input(\"Enter the name of a product that a customer wants to order:\")\n", + " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", + " raise TypeError(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", + " elif inventory[order] <= 0:\n", + " raise ValueError(f\"The product is not in stock! Our current stock is: {inventory}\")\n", + " except TypeError as t:\n", + " print(t)\n", + " except ValueError as v:\n", + " print(v)\n", " else:\n", - " customer_orders.add(order)\n", - " valid_input = True\n", + " customer_orders.add(order.lower())\n", + " valid_input = True\n", " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 33, "id": "d30e75c9", "metadata": {}, "outputs": [], "source": [ "def update_inventory(customer_orders, inventory):\n", " for product in customer_orders:\n", - " if product in inventory.keys():\n", - " inventory[product] -= 1\n", + " try:\n", + " inventory[product]\n", + " except KeyError as k:\n", + " print(\"At least one of the products in the customer order is no in the inventory\")\n", + " else:\n", + " for product in inventory.keys():\n", + " inventory[product] -= 1\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 34, "id": "741c83b5", "metadata": {}, "outputs": [], "source": [ "def calculate_order_statistics(customer_orders, products):\n", " total_products_ordered = len(customer_orders)\n", - " perc = (total_products_ordered / len(products)) * 100\n", + " try:\n", + " perc = (total_products_ordered / len(products)) * 100\n", + " except ZeroDivisionError:\n", + " print(\"Please add products to the list of products.\")\n", " order_statistics = [total_products_ordered, int(perc)]\n", " return order_statistics" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 35, "id": "b7e0e106", "metadata": {}, "outputs": [], @@ -153,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 36, "id": "f109be01", "metadata": {}, "outputs": [], @@ -164,9 +180,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 37, "id": "99d37db8-caec-43fc-b34e-fd9ecf57d78d", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdin", @@ -179,32 +197,99 @@ "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n" + "Invalid input. Please enter a non negative number.\n", + "Currently, these are the products in the inventory: {}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: 02\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of mug available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a non negative number.\n", + "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of mug available: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of hat available: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of t-shirt available: -3\n" + "Please provide number of book available: 9\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n" + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of keychain available: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of t-shirt available: 7\n", - "Please provide number of mug available: 2\n", - "Please provide number of hat available: 1\n", - "Please provide number of book available: 34\n", - "Please provide number of keychain available: 5\n", "Enter the number of customer orders: no\n" ] }, @@ -248,7 +333,20 @@ "output_type": "stream", "text": [ "Enter the number of customer orders: 3\n", - "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The product is not in stock! Our current stock is: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ "Enter the name of a product that a customer wants to order: laptop\n" ] }, @@ -263,7 +361,8 @@ "name": "stdin", "output_type": "stream", "text": [ - "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: t-shirt\n", + "Enter the name of a product that a customer wants to order: hat\n", "Enter the name of a product that a customer wants to order: book\n" ] }, @@ -272,7 +371,7 @@ "output_type": "stream", "text": [ "The total number of products ordered is 3, and the percentage of unique products ordered is 60%\n", - "The updated inventory is: {'t-shirt': 7, 'mug': 1, 'hat': 0, 'book': 33, 'keychain': 5}\n" + "The updated inventory is: {'t-shirt': -1, 'mug': -3, 'hat': 31, 'book': 6, 'keychain': 2}\n" ] } ], From 0258f7a5cd6bc8fe82a9e7cc8b5b506b008596e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tebas=20Mart=C3=ADnez?= <203688711+TebasMartinez@users.noreply.github.com> Date: Sun, 13 Jul 2025 00:54:03 +0200 Subject: [PATCH 3/3] correct errors pointed by lab feedback --- ...lab-python-error-handling-checkpoint.ipynb | 90 ++++++++----------- lab-python-error-handling.ipynb | 90 ++++++++----------- 2 files changed, 72 insertions(+), 108 deletions(-) diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb index 2f0aec0..ed67eee 100644 --- a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 47, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 48, "id": "1933703b", "metadata": {}, "outputs": [], @@ -70,8 +70,8 @@ " while not valid_input:\n", " try:\n", " qty = input(f\"Please provide number of {product} available: \")\n", - " if not qty.isnumeric():\n", - " raise ValueError(\"Invalid input. Please enter a non negative number.\") \n", + " if not qty.isnumeric() or int(qty) < 0:\n", + " raise ValueError(\"Invalid input. Please enter a non-negative number.\") \n", " except ValueError as v:\n", " print(v)\n", " else:\n", @@ -84,7 +84,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 49, "id": "f81c9580", "metadata": {}, "outputs": [], @@ -105,24 +105,22 @@ " valid_input = False\n", " while not valid_input:\n", " try:\n", - " order = input(\"Enter the name of a product that a customer wants to order:\")\n", - " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", + " order = input(\"Enter the name of a product that a customer wants to order:\").lower()\n", + " if order not in [word.lower() for word in inventory.keys()]:\n", " raise TypeError(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", " elif inventory[order] <= 0:\n", " raise ValueError(f\"The product is not in stock! Our current stock is: {inventory}\")\n", - " except TypeError as t:\n", - " print(t)\n", - " except ValueError as v:\n", - " print(v)\n", + " except (TypeError, ValueError) as e:\n", + " print(e)\n", " else:\n", - " customer_orders.add(order.lower())\n", + " customer_orders.add(order)\n", " valid_input = True\n", " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 50, "id": "d30e75c9", "metadata": {}, "outputs": [], @@ -130,18 +128,15 @@ "def update_inventory(customer_orders, inventory):\n", " for product in customer_orders:\n", " try:\n", - " inventory[product]\n", + " inventory[product] -= 1\n", " except KeyError as k:\n", " print(\"At least one of the products in the customer order is no in the inventory\")\n", - " else:\n", - " for product in inventory.keys():\n", - " inventory[product] -= 1\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 51, "id": "741c83b5", "metadata": {}, "outputs": [], @@ -151,14 +146,15 @@ " try:\n", " perc = (total_products_ordered / len(products)) * 100\n", " except ZeroDivisionError:\n", - " print(\"Please add products to the list of products.\")\n", + " print(\"Product list is empty.\")\n", + " perc = 0\n", " order_statistics = [total_products_ordered, int(perc)]\n", " return order_statistics" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 52, "id": "b7e0e106", "metadata": {}, "outputs": [], @@ -169,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 53, "id": "f109be01", "metadata": {}, "outputs": [], @@ -180,7 +176,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 54, "id": "99d37db8-caec-43fc-b34e-fd9ecf57d78d", "metadata": { "scrolled": true @@ -197,7 +193,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n", + "Invalid input. Please enter a non-negative number.\n", "Currently, these are the products in the inventory: {}\n" ] }, @@ -205,29 +201,29 @@ "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of t-shirt available: 02\n" + "Please provide number of t-shirt available: -2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + "Invalid input. Please enter a non-negative number.\n", + "Currently, these are the products in the inventory: {}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of mug available: -2\n" + "Please provide number of t-shirt available: 3\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n", - "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3}\n" ] }, { @@ -241,56 +237,56 @@ "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of hat available: 34\n" + "Please provide number of hat available: 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0, 'hat': 2}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of book available: 9\n" + "Please provide number of book available: 34\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0, 'hat': 2, 'book': 34}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of keychain available: 5\n" + "Please provide number of keychain available: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0, 'hat': 2, 'book': 34, 'keychain': 1}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the number of customer orders: no\n" + "Enter the number of customer orders: none\n" ] }, { @@ -340,30 +336,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "The product is not in stock! Our current stock is: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the name of a product that a customer wants to order: laptop\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The product is not in the inventory. Products available are: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + "The product is not in stock! Our current stock is: {'t-shirt': 3, 'mug': 0, 'hat': 2, 'book': 34, 'keychain': 1}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the name of a product that a customer wants to order: t-shirt\n", "Enter the name of a product that a customer wants to order: hat\n", - "Enter the name of a product that a customer wants to order: book\n" + "Enter the name of a product that a customer wants to order: book\n", + "Enter the name of a product that a customer wants to order: keychain\n" ] }, { @@ -371,7 +353,7 @@ "output_type": "stream", "text": [ "The total number of products ordered is 3, and the percentage of unique products ordered is 60%\n", - "The updated inventory is: {'t-shirt': -1, 'mug': -3, 'hat': 31, 'book': 6, 'keychain': 2}\n" + "The updated inventory is: {'t-shirt': 3, 'mug': 0, 'hat': 1, 'book': 33, 'keychain': 0}\n" ] } ], diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 2f0aec0..ed67eee 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 47, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 48, "id": "1933703b", "metadata": {}, "outputs": [], @@ -70,8 +70,8 @@ " while not valid_input:\n", " try:\n", " qty = input(f\"Please provide number of {product} available: \")\n", - " if not qty.isnumeric():\n", - " raise ValueError(\"Invalid input. Please enter a non negative number.\") \n", + " if not qty.isnumeric() or int(qty) < 0:\n", + " raise ValueError(\"Invalid input. Please enter a non-negative number.\") \n", " except ValueError as v:\n", " print(v)\n", " else:\n", @@ -84,7 +84,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 49, "id": "f81c9580", "metadata": {}, "outputs": [], @@ -105,24 +105,22 @@ " valid_input = False\n", " while not valid_input:\n", " try:\n", - " order = input(\"Enter the name of a product that a customer wants to order:\")\n", - " if order.lower() not in [word.lower() for word in inventory.keys()]:\n", + " order = input(\"Enter the name of a product that a customer wants to order:\").lower()\n", + " if order not in [word.lower() for word in inventory.keys()]:\n", " raise TypeError(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n", " elif inventory[order] <= 0:\n", " raise ValueError(f\"The product is not in stock! Our current stock is: {inventory}\")\n", - " except TypeError as t:\n", - " print(t)\n", - " except ValueError as v:\n", - " print(v)\n", + " except (TypeError, ValueError) as e:\n", + " print(e)\n", " else:\n", - " customer_orders.add(order.lower())\n", + " customer_orders.add(order)\n", " valid_input = True\n", " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 50, "id": "d30e75c9", "metadata": {}, "outputs": [], @@ -130,18 +128,15 @@ "def update_inventory(customer_orders, inventory):\n", " for product in customer_orders:\n", " try:\n", - " inventory[product]\n", + " inventory[product] -= 1\n", " except KeyError as k:\n", " print(\"At least one of the products in the customer order is no in the inventory\")\n", - " else:\n", - " for product in inventory.keys():\n", - " inventory[product] -= 1\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 51, "id": "741c83b5", "metadata": {}, "outputs": [], @@ -151,14 +146,15 @@ " try:\n", " perc = (total_products_ordered / len(products)) * 100\n", " except ZeroDivisionError:\n", - " print(\"Please add products to the list of products.\")\n", + " print(\"Product list is empty.\")\n", + " perc = 0\n", " order_statistics = [total_products_ordered, int(perc)]\n", " return order_statistics" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 52, "id": "b7e0e106", "metadata": {}, "outputs": [], @@ -169,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 53, "id": "f109be01", "metadata": {}, "outputs": [], @@ -180,7 +176,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 54, "id": "99d37db8-caec-43fc-b34e-fd9ecf57d78d", "metadata": { "scrolled": true @@ -197,7 +193,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n", + "Invalid input. Please enter a non-negative number.\n", "Currently, these are the products in the inventory: {}\n" ] }, @@ -205,29 +201,29 @@ "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of t-shirt available: 02\n" + "Please provide number of t-shirt available: -2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + "Invalid input. Please enter a non-negative number.\n", + "Currently, these are the products in the inventory: {}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of mug available: -2\n" + "Please provide number of t-shirt available: 3\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a non negative number.\n", - "Currently, these are the products in the inventory: {'t-shirt': 2}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3}\n" ] }, { @@ -241,56 +237,56 @@ "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of hat available: 34\n" + "Please provide number of hat available: 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0, 'hat': 2}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of book available: 9\n" + "Please provide number of book available: 34\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0, 'hat': 2, 'book': 34}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Please provide number of keychain available: 5\n" + "Please provide number of keychain available: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Currently, these are the products in the inventory: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" + "Currently, these are the products in the inventory: {'t-shirt': 3, 'mug': 0, 'hat': 2, 'book': 34, 'keychain': 1}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the number of customer orders: no\n" + "Enter the number of customer orders: none\n" ] }, { @@ -340,30 +336,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "The product is not in stock! Our current stock is: {'t-shirt': 2, 'mug': 0, 'hat': 34, 'book': 9, 'keychain': 5}\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the name of a product that a customer wants to order: laptop\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The product is not in the inventory. Products available are: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + "The product is not in stock! Our current stock is: {'t-shirt': 3, 'mug': 0, 'hat': 2, 'book': 34, 'keychain': 1}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the name of a product that a customer wants to order: t-shirt\n", "Enter the name of a product that a customer wants to order: hat\n", - "Enter the name of a product that a customer wants to order: book\n" + "Enter the name of a product that a customer wants to order: book\n", + "Enter the name of a product that a customer wants to order: keychain\n" ] }, { @@ -371,7 +353,7 @@ "output_type": "stream", "text": [ "The total number of products ordered is 3, and the percentage of unique products ordered is 60%\n", - "The updated inventory is: {'t-shirt': -1, 'mug': -3, 'hat': 31, 'book': 6, 'keychain': 2}\n" + "The updated inventory is: {'t-shirt': 3, 'mug': 0, 'hat': 1, 'book': 33, 'keychain': 0}\n" ] } ],