From 5ed5ef831b1d5e43f452ec6ff46bc104f96140d4 Mon Sep 17 00:00:00 2001 From: GarriguesElena Date: Sat, 20 Jun 2026 09:18:51 +0200 Subject: [PATCH] error handling lab solved --- ...lab-python-error-handling-checkpoint.ipynb | 366 ++++++++++++++++++ lab-python-error-handling.ipynb | 274 ++++++++++++- 2 files changed, 637 insertions(+), 3 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..7f77d8d --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,366 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b", + "metadata": {}, + "source": [ + "## Exercise: Error Handling for Managing Customer Orders\n", + "\n", + "The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n", + "\n", + "For example, we could modify the `initialize_inventory` function to include error handling.\n", + " - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n", + "\n", + "```python\n", + "# Step 1: Define the function for initializing the inventory with error handling\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "# Or, in another way:\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", + "\n", + "Let's enhance your code by implementing error handling to handle invalid inputs.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "2. Modify the `calculate_total_price` function to include error handling.\n", + " - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n", + "\n", + "3. Modify the `get_customer_orders` function to include error handling.\n", + " - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n", + " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. *Hint: you will need to pass inventory as a parameter*\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n", + "\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": 1, + "id": "a5454049-afc9-4c79-a378-1b47a7c50fa6", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "\n", + "def initialize_inventory(products):\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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "58403830-1627-4f70-be52-4b00173597e4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 11\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 1\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 6\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 11, 'mug': 4, 'hat': 1, 'book': 3, 'keychain': 6}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "833edc2b-8429-4286-bdae-2a197032b36a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 10, 'mug': 3, 'book': 3, 'keychain': 6}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders = {\"hat\", \"mug\", \"t-shirt\"}\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {product: inventory[product]- 1 if product in customer_orders else inventory[product] for product in inventory}\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " \n", + " return inventory\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "03785510-8837-4c85-964b-c23c9b6502a7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price for mug: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter numeric value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price for mug: 1\n", + "Enter the price for t-shirt: 3\n", + "Enter the price for hat: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_total_price(customer_orders):\n", + " valid_input = False\n", + " while not valid_input: \n", + " try:\n", + " total_price = sum([int(input(f\"Enter the price for {product}: \")) for product in customer_orders])\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter numeric value.\")\n", + " return total_price\n", + "\n", + "calculate_total_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "186a3753-0216-4fc3-8695-17a95e3632ac", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many items would you like to order? f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter numeric value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many items would you like to order? 2\n", + "Enter the product name: mug\n", + "Enter the product name: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product, not available in the inventory. Please enter an existing product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product, not available in the inventory. Please enter an existing product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: t-shirt\n" + ] + }, + { + "data": { + "text/plain": [ + "{'mug', 't-shirt'}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " number_of_orders = int(input(\"How many items would you like to order?\"))\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter numeric value.\")\n", + " \n", + " customer_orders = set()\n", + "\n", + " for i in range(number_of_orders):\n", + " while True:\n", + " product = input(\"Enter the product name: \")\n", + " \n", + " try:\n", + " inventory[product]\n", + " customer_orders.add(product)\n", + " break\n", + " \n", + " except KeyError:\n", + " print(\"Invalid product, not available in the inventory. Please enter an existing product.\")\n", + " \n", + " return customer_orders\n", + "\n", + "get_customer_orders(inventory)" + ] + } + ], + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..7f77d8d 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,281 @@ "\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": 1, + "id": "a5454049-afc9-4c79-a378-1b47a7c50fa6", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "\n", + "def initialize_inventory(products):\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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "58403830-1627-4f70-be52-4b00173597e4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 11\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 1\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 6\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 11, 'mug': 4, 'hat': 1, 'book': 3, 'keychain': 6}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "833edc2b-8429-4286-bdae-2a197032b36a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 10, 'mug': 3, 'book': 3, 'keychain': 6}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders = {\"hat\", \"mug\", \"t-shirt\"}\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {product: inventory[product]- 1 if product in customer_orders else inventory[product] for product in inventory}\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " \n", + " return inventory\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "03785510-8837-4c85-964b-c23c9b6502a7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price for mug: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter numeric value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price for mug: 1\n", + "Enter the price for t-shirt: 3\n", + "Enter the price for hat: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_total_price(customer_orders):\n", + " valid_input = False\n", + " while not valid_input: \n", + " try:\n", + " total_price = sum([int(input(f\"Enter the price for {product}: \")) for product in customer_orders])\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter numeric value.\")\n", + " return total_price\n", + "\n", + "calculate_total_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "186a3753-0216-4fc3-8695-17a95e3632ac", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many items would you like to order? f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter numeric value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many items would you like to order? 2\n", + "Enter the product name: mug\n", + "Enter the product name: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product, not available in the inventory. Please enter an existing product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product, not available in the inventory. Please enter an existing product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: t-shirt\n" + ] + }, + { + "data": { + "text/plain": [ + "{'mug', 't-shirt'}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " number_of_orders = int(input(\"How many items would you like to order?\"))\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter numeric value.\")\n", + " \n", + " customer_orders = set()\n", + "\n", + " for i in range(number_of_orders):\n", + " while True:\n", + " product = input(\"Enter the product name: \")\n", + " \n", + " try:\n", + " inventory[product]\n", + " customer_orders.add(product)\n", + " break\n", + " \n", + " except KeyError:\n", + " print(\"Invalid product, not available in the inventory. Please enter an existing product.\")\n", + " \n", + " return customer_orders\n", + "\n", + "get_customer_orders(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": { @@ -90,7 +358,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,