diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..6c24371 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,97 @@ "\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": "8f511565", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products): \n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False \n", + " while not valid_quantity: #Le programme revient ensuite au début de la boucle while et demande à l'utilisateur de saisir un nouveau nombre.\n", + " try: # Par exemple, si on demande à l'utilisateur de saisir un nombre, mais qu'il saisit un texte au lieu d'un nombre, cela peut causer une erreur. Pour éviter cela, on peut utiliser un bloc try/except pour gérer l'erreur, et afficher un message d'erreur personnalisé à l'utilisateur. # le bloc try contient le code qui peut causer une erreur, c'est-à-dire la conversion de la saisie utilisateur en un entier.\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: #Si une erreur se produit, le bloc except est exécuté et affiche un message d'erreur personnalisé. \n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory #Le programme revient ensuite au début de la boucle while et demande à l'utilisateur de saisir un nouveau nombre.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "0ff91b22", + "metadata": {}, + "source": [ + "#( replace “inventory” with price) #calculate_total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fe2af297", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products): \n", + " price = {}\n", + " for product in products:\n", + " valid_price = False \n", + " while not valid_price: #The program then returns to the beginning of the `while` loop and prompts the user to enter a new number.\n", + " try: # The try block contains the code that may cause an error, namely converting the user input to an integer.\n", + " price = float(input(f\"Enter the price of {product}s available: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\") \n", + " valid_price = True\n", + " except ValueError as error: #If an error occurs, the `except` block is executed and displays a custom error message. \n", + " print(f\"Error: {error}\")\n", + " inventory[product] = price\n", + " return price #The program then returns to the beginning of the `while` loop and prompts the user to enter a new number." + ] + }, + { + "cell_type": "markdown", + "id": "fec3430a", + "metadata": {}, + "source": [ + "#( replace “inventory” with orders) #get_customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6044594b", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(products): \n", + " orders = {}\n", + " for product in products:\n", + " valid_orders = False \n", + " while not valid_orders: #The program then returns to the beginning of the `while` loop and prompts the user to enter a new number.\n", + " try: # The try block contains the code that may cause an error, namely converting the user input to an integer.\n", + " orders = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if orders < 0:\n", + " raise ValueError(\"Invalid orders! Please enter a non-negative value.\")\n", + " valid_orders = True\n", + " except ValueError as error: #If an error occurs, the `except` block is executed and displays a custom error message. \n", + " print(f\"Error: {error}\")\n", + " orders[product] = orders\n", + " return orders #The program then returns to the beginning of the `while` loop and prompts the user to enter a new number." + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +176,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4,