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..7dae3ea --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,461 @@ +{ + "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": 3, + "id": "db1f2041-46af-4aa5-ada7-5d87db591042", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "b7af63bd-5e55-4241-b0ae-79dd6beb9ac3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 0\n", + "Enter the quantity of mugs available: u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 1\n", + "Enter the quantity of hats available: 10\n", + "Enter the quantity of books available: 14\n", + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 1, 'hat': 10, 'book': 14, 'keychain': 5}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4184c1a6-3232-4d40-b906-c81f7104005a", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = ['mug', 'keychain']" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5c0b5fed-f986-48d4-ad26-2b5f4200f836", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 2\n", + "Enter the product: book\n", + "Enter the product: hat\n" + ] + }, + { + "data": { + "text/plain": [ + "['book', 'hat']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders():\n", + " #customer_orders = {}\n", + " valid_nb = False\n", + " while not valid_nb:\n", + " try:\n", + " nb = int(input(\"How many products do you want to order? \"))\n", + " if nb < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_nb = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " else: \n", + " customer_orders = [input(\"Enter the product: \") for i in range(nb)] # if input in products don't find how to do it\n", + " return customer_orders\n", + " \n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "05185fa2-1cf5-484b-9bda-fca8dcb66746", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 2\n", + "Enter the name of a product you want to order: table\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: This product is not in our inventory, or you did not write it correctly sorry\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product you want to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat added to the customer list!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product you want to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "book added to the customer list!\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat'}" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " valid_nb = False\n", + " while not valid_nb:\n", + " try:\n", + " nb = int(input(\"How many products do you want to order? \"))\n", + " if nb < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " else:\n", + " valid_nb = True\n", + " for i in range(nb):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input('Enter the name of a product you want to order: ')\n", + " if product not in products:\n", + " raise ValueError(\"This product is not in our inventory, or you did not write it correctly sorry\")\n", + " except ValueError as error: # erreur corrigée suite à review AI sur ce cas \n", + " print(f\"Error: {error}\") \n", + " else:\n", + " valid_product = True\n", + " print(f\"{product} added to the customer list!\")\n", + " customer_orders.add(product)\n", + " return customer_orders\n", + " print(\"Operation complete\")\n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "59836803-93cd-4bb8-a4f3-b7c068178709", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of the ordered mug: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid price! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of the ordered mug: u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of the ordered mug: 1\n", + "Enter the price of the ordered keychain: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# calculate price\n", + " \n", + "def calculate_price(customer_orders):\n", + " list_prices = []\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = int(input(f'Enter the price of the ordered {product}: '))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " list_prices.append(price)\n", + " total = sum(list_prices)\n", + " return total\n", + "\n", + "calculate_price(customer_orders)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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..7dae3ea 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,369 @@ "\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": "db1f2041-46af-4aa5-ada7-5d87db591042", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "b7af63bd-5e55-4241-b0ae-79dd6beb9ac3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 0\n", + "Enter the quantity of mugs available: u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 1\n", + "Enter the quantity of hats available: 10\n", + "Enter the quantity of books available: 14\n", + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 1, 'hat': 10, 'book': 14, 'keychain': 5}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4184c1a6-3232-4d40-b906-c81f7104005a", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = ['mug', 'keychain']" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5c0b5fed-f986-48d4-ad26-2b5f4200f836", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 2\n", + "Enter the product: book\n", + "Enter the product: hat\n" + ] + }, + { + "data": { + "text/plain": [ + "['book', 'hat']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders():\n", + " #customer_orders = {}\n", + " valid_nb = False\n", + " while not valid_nb:\n", + " try:\n", + " nb = int(input(\"How many products do you want to order? \"))\n", + " if nb < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_nb = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " else: \n", + " customer_orders = [input(\"Enter the product: \") for i in range(nb)] # if input in products don't find how to do it\n", + " return customer_orders\n", + " \n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "05185fa2-1cf5-484b-9bda-fca8dcb66746", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 2\n", + "Enter the name of a product you want to order: table\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: This product is not in our inventory, or you did not write it correctly sorry\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product you want to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat added to the customer list!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product you want to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "book added to the customer list!\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat'}" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " valid_nb = False\n", + " while not valid_nb:\n", + " try:\n", + " nb = int(input(\"How many products do you want to order? \"))\n", + " if nb < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " else:\n", + " valid_nb = True\n", + " for i in range(nb):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input('Enter the name of a product you want to order: ')\n", + " if product not in products:\n", + " raise ValueError(\"This product is not in our inventory, or you did not write it correctly sorry\")\n", + " except ValueError as error: # erreur corrigée suite à review AI sur ce cas \n", + " print(f\"Error: {error}\") \n", + " else:\n", + " valid_product = True\n", + " print(f\"{product} added to the customer list!\")\n", + " customer_orders.add(product)\n", + " return customer_orders\n", + " print(\"Operation complete\")\n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "59836803-93cd-4bb8-a4f3-b7c068178709", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of the ordered mug: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid price! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of the ordered mug: u\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'u'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of the ordered mug: 1\n", + "Enter the price of the ordered keychain: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# calculate price\n", + " \n", + "def calculate_price(customer_orders):\n", + " list_prices = []\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = int(input(f'Enter the price of the ordered {product}: '))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " list_prices.append(price)\n", + " total = sum(list_prices)\n", + " return total\n", + "\n", + "calculate_price(customer_orders)" + ] } ], "metadata": { @@ -90,7 +453,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,