diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..0bfdc5a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,13 +41,143 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f'Choose quantity of {product}: '))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Invalid input: {e}. Please enter a non-negative integer.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders(products):\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " try:\n", + " product = input(\"Enter the name of a product that the customer wants to order: \")\n", + " if product not in products:\n", + " raise ValueError(\"Invalid product name.\")\n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want to add another product? (yes/no): \")\n", + " if answer.lower() in ['no', 'n']:\n", + " break\n", + " except ValueError as e:\n", + " print(e)\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " try:\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Out of stock for {product}.\")\n", + " else:\n", + " raise KeyError(f\"{product} is not in the inventory.\")\n", + " except KeyError as e:\n", + " print(e)\n", + " return inventory\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " try:\n", + " total_products_ordered = len(customer_orders)\n", + " if len(products) == 0:\n", + " raise ValueError(\"Products list cannot be empty.\")\n", + " unique_products = set(products)\n", + " percentage = (total_products_ordered / len(unique_products)) * 100\n", + " return total_products_ordered, percentage\n", + " except ZeroDivisionError:\n", + " print(\"No products available to calculate percentage.\")\n", + " return 0, 0\n", + " except ValueError as e:\n", + " print(e)\n", + " return 0, 0\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " try:\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}%\")\n", + " except Exception as e:\n", + " print(f\"An error occurred while printing order statistics: {e}\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " try:\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + " except Exception as e:\n", + " print(f\"An error occurred while printing updated inventory: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0cea9bf3-ee43-4903-954c-efe00d4aeaa4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose quantity of t-shirt: 5\n", + "Choose quantity of mug: 5\n", + "Choose quantity of hat: 5\n", + "Choose quantity of book: 5\n", + "Choose quantity of keychain: 5\n", + "Enter the name of a product that the customer wants to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product that the customer wants to order: hat\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 4\n", + "book: 5\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(products)\n", + "updated_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(updated_inventory)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15ccfe7f-1523-4128-b189-ce63f7b8950f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -66,7 +196,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,