diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..bbf56cb 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,315 @@ "\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": 9, + "id": "1e7d0648-36ec-40fc-b872-5f9987035378", + "metadata": {}, + "outputs": [], + "source": [ + "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": 10, + "id": "8914fb53-1de4-402d-8ecd-cd054f6ee66e", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " number_of_orders = 0\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " number_of_orders = int(input(\"How many products does the customer want to order? \"))\n", + " if number_of_orders < 0:\n", + " print(\"Error: number of orders cannot be negative.\")\n", + " else:\n", + " valid_number = True\n", + " except ValueError:\n", + " print(\"Error: please enter a valid number.\")\n", + " customer_orders = set()\n", + " for _ in range(number_of_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " item = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + " if item not in inventory:\n", + " print(\"Error: product is not in the inventory.\")\n", + " elif inventory[item] <= 0:\n", + " print(\"Error: product is out of stock.\")\n", + " else:\n", + " customer_orders.add(item)\n", + " valid_product = True\n", + " except ValueError:\n", + " print(\"Error: invalid product.\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "0f8faefc-d716-41ec-9eb9-d9763b87c192", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " inventory = {item: inventory[item] - 1 if item in customer_orders else inventory[item]for item in inventory}\n", + " inventory = {item: quantity for item, quantity in inventory.items() if quantity > 0}\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f870ff34-1824-4447-a8e2-07ba965279ac", + "metadata": {}, + "outputs": [], + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "36e4dcb9-0282-4e31-8693-f88dbea98829", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_products_ordered = order_statistics\n", + " labels = [\"Total Products Ordered:\", \"Percentage of Products Ordered:\"]\n", + " print()\n", + " print(\"Order Statistics:\")\n", + " for label, value in zip(labels, order_statistics):\n", + " print(label, value)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "570cb1c5-88dd-4de4-90b3-6f573788ba81", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " updated_inventory = {item.title(): quantity for item, quantity in inventory.items()}\n", + " print()\n", + " print(\"Updated Inventory:\")\n", + " for item, quantity in updated_inventory.items():\n", + " print(item + \":\", quantity)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "281e47b4-9e78-4b43-a378-8cbd766e2ae2", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " prices = []\n", + " for item in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {item}: \"))\n", + " if price < 0:\n", + " print(\"Error: price cannot be negative.\")\n", + " else:\n", + " prices.append(price)\n", + " valid_price = True\n", + " except ValueError:\n", + " print(\"Error: please enter a valid number.\")\n", + " total_price = sum(prices)\n", + " print(\"Total Price:\", total_price)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "de4f1d02-2c30-4efe-b361-5b399b5ed5c7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'w'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: -4\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 t-shirts available: 4\n", + "Enter the quantity of mugs available: 3\n", + "Enter the quantity of hats available: 2\n", + "Enter the quantity of books available: 1\n", + "Enter the quantity of keychains available: 0\n", + "How many products does the customer want to order? w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products does the customer want to order? -5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: number of orders cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products does the customer want to order? 3\n", + "Enter the name of a product that a customer wants to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product is out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: book\n", + "Enter the name of a product that a customer wants to order: book\n", + "Enter the name of a product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0\n", + "\n", + "Updated Inventory:\n", + "T-Shirt: 4\n", + "Mug: 3\n", + "Hat: 2\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: -5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: price cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 10.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "calculate_total_price(customer_orders)" + ] } ], "metadata": { @@ -90,7 +399,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,