diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..15966a0 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,18 +41,123 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try: \n", + " quantity = int(input(f\"Enter the initial quantity for {product}: \"))\n", + "\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative. Please enter a valid number.\")\n", + " \n", + " except ValueError as e:\n", + " print(e)\n", + "\n", + " else:\n", + " inventory[product] = quantity\n", + " break \n", + " return inventory\n", + "\n", + "\n", + "def get_customer_orders(products):\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " try:\n", + " order = input(\"Enter the name of a product the customer wants: \").lower().strip()\n", + "\n", + " if order == \"\":\n", + " raise ValueError(\"Product name cannot be empty. Please enter a valid product name.\")\n", + "\n", + " if order in products:\n", + " customer_orders.add(order)\n", + "\n", + " else:\n", + " print(\"That product does not exist in the store.\")\n", + " continue\n", + "\n", + " except ValueError as e:\n", + " print(e)\n", + " continue\n", + "\n", + " while True:\n", + " another = input(\"Does the customer want to order another product? (yes/no): \").lower().strip()\n", + " if another in [\"yes\", \"no\"]:\n", + " break\n", + " print(\"Please enter 'yes' or 'no'.\")\n", + "\n", + " if another == \"no\":\n", + " break \n", + " \n", + "\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + "\n", + " for product in customer_orders:\n", + " try:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Sorry, {product} is out of stock\")\n", + " except KeyError:\n", + " print(f\"Sorry, {product} is not in the inventory.\")\n", + " \n", + " \n", + "def calculate_order_statistics(customer_orders, products):\n", + " try:\n", + " total_customer_orders = len(customer_orders)\n", + " total_products = len(products)\n", + " percentage_order = (total_customer_orders / total_products) * 100\n", + "\n", + " except ZeroDivisionError:\n", + " print(\"No products available in the store to calculate statistics.\")\n", + " return 0, 0\n", + "\n", + " else:\n", + " return total_customer_orders, percentage_order \n", + "\n", + "def print_order_statistics(order_statistics):\n", + " try:\n", + " total = order_statistics[0]\n", + " percentage = order_statistics[1]\n", + "\n", + " print(\"total orders:\", total)\n", + " print(\"percentage:\", percentage)\n", + "\n", + " except IndexError:\n", + " print(\"Index out of range for order statistics.\")\n", + "\n", + " except TypeError:\n", + " print(\"Invalid data type for order statistics.\")\n", + "\n", + " else:\n", + " print(\"Order statistics printed successfully.\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " try:\n", + " print(\"Inventory:\")\n", + " for product in inventory:\n", + " print(product, inventory[product])\n", + "\n", + " except Exception as e:\n", + " print(\"Error while printing inventory:\")\n", + " print(e)\n", + " \n", + " " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -66,7 +171,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,