diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..3d74244 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,170 @@ "\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": "31b91ab7", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "\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": 2, + "id": "4b3299be", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0.0\n", + " \n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price for {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", + "\n", + " total_price += price\n", + " \n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3ac91d46", + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"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", + "\"\"\"\n", + "\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"The number of orders cannot be negative. Please enter a non-negative integer.\")\n", + " break \n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " for i in range(num_orders):\n", + " while True:\n", + " product = input(f\"Enter the name of product {i + 1} the customer wants to order: \").strip()\n", + " if product not in inventory:\n", + " print(f\"Error: {product} is not in the inventory. Please enter a valid product name.\")\n", + " elif inventory[product] <= 0:\n", + " print(f\"Error: {product} is out of stock. Please choose another product.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " break\n", + " \n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "020ca311", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'g'\n", + "Error: 4 is not in the inventory. Please enter a valid product name.\n", + "Error: 5 is not in the inventory. Please enter a valid product name.\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0 %\n", + "Updated Inventory:\n", + "t-shirts: 5\n", + "mugs: 2\n", + "hats: 4\n", + "books: 6\n", + "keychains: 4\n", + "Error: could not convert string to float: 't-shirt'\n", + "Error: could not convert string to float: 'nbook'\n", + "Error: could not convert string to float: 'keychain'\n", + "Total Price of Customer Order: 9.0\n" + ] + } + ], + "source": [ + "#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", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {product: (quantity - 1 if product in customer_orders and quantity > 0 else quantity)\n", + " for product, quantity in inventory.items()}\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " return inventory\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_products_ordered\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total)\n", + " print(\"Percentage of Unique Products Ordered:\", percentage, \"%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " inventory_details = [product + \"s: \" + str(quantity) for product, quantity in inventory.items()]\n", + " print(\"\\n\".join(inventory_details))\n", + "\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)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(\"Total Price of Customer Order:\", total_price)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +249,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,