diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c38994b 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,525 @@ "\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": "markdown", + "id": "f724b67d", + "metadata": {}, + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "\n", + "def get_valid_product(inventory):\n", + " while True:\n", + " try:\n", + " product_name = input(\"Enter the name of the product you want to order: \").strip().lower()\n", + "\n", + " if product_name not in inventory:\n", + " raise ValueError(\"This product does not exist in the inventory.\")\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(\"This product is out of stock.\")\n", + "\n", + " return product_name\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = {get_valid_product(inventory) for _ in range(number_of_orders)}\n", + " return customer_orders\n", + "\n", + "\n", + "def get_valid_price(product):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " return price\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = [get_valid_price(product) for product in customer_orders]\n", + " return sum(prices)\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " if (quantity - 1 if product in customer_orders else quantity) > 0\n", + " }\n", + " return inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_unique_products_ordered = (\n", + " 0 if len(products) == 0 else (total_products_ordered / len(products)) * 100\n", + " )\n", + " return total_products_ordered, percentage_of_unique_products_ordered\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_of_unique_products_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_of_unique_products_ordered}\")\n", + "\n", + "\n", + "def print_inventory(inventory, title):\n", + " print(title)\n", + " lines = [f\"{product}: {quantity}\" for product, quantity in inventory.items()]\n", + " print(\"\\n\".join(lines))\n", + "\n", + "\n", + "def main():\n", + " inventory = initialize_inventory(products)\n", + " print()\n", + " print_inventory(inventory, \"Initial Inventory:\")\n", + " print()\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + " print()\n", + "\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print()\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " print_inventory(inventory, \"Updated Inventory:\")\n", + " print()\n", + "\n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"Total Price: {total_price}\")\n", + "\n", + "\n", + "main()" + ] + }, + { + "cell_type": "markdown", + "id": "861fd28b", + "metadata": {}, + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "\n", + "def get_valid_product(inventory):\n", + " while True:\n", + " try:\n", + " product_name = input(\"Enter the name of the product you want to order: \").strip().lower()\n", + "\n", + " if product_name not in inventory:\n", + " raise ValueError(\"This product does not exist in the inventory.\")\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(\"This product is out of stock.\")\n", + "\n", + " return product_name\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = {get_valid_product(inventory) for _ in range(number_of_orders)}\n", + " return customer_orders\n", + "\n", + "\n", + "def get_valid_price(product):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " return price\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = [get_valid_price(product) for product in customer_orders]\n", + " return sum(prices)\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " if (quantity - 1 if product in customer_orders else quantity) > 0\n", + " }\n", + " return inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_unique_products_ordered = (\n", + " 0 if len(products) == 0 else (total_products_ordered / len(products)) * 100\n", + " )\n", + " return total_products_ordered, percentage_of_unique_products_ordered\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_of_unique_products_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_of_unique_products_ordered}\")\n", + "\n", + "\n", + "def print_inventory(inventory, title):\n", + " print(title)\n", + " lines = [f\"{product}: {quantity}\" for product, quantity in inventory.items()]\n", + " print(\"\\n\".join(lines))\n", + "\n", + "\n", + "def main():\n", + " inventory = initialize_inventory(products)\n", + " print()\n", + " print_inventory(inventory, \"Initial Inventory:\")\n", + " print()\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + " print()\n", + "\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print()\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " print_inventory(inventory, \"Updated Inventory:\")\n", + " print()\n", + "\n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"Total Price: {total_price}\")\n", + "\n", + "\n", + "main()" + ] + }, + { + "cell_type": "markdown", + "id": "43d49049", + "metadata": {}, + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "\n", + "def get_valid_product(inventory):\n", + " while True:\n", + " try:\n", + " product_name = input(\"Enter the name of the product you want to order: \").strip().lower()\n", + "\n", + " if product_name not in inventory:\n", + " raise ValueError(\"This product does not exist in the inventory.\")\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(\"This product is out of stock.\")\n", + "\n", + " return product_name\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = {get_valid_product(inventory) for _ in range(number_of_orders)}\n", + " return customer_orders\n", + "\n", + "\n", + "def get_valid_price(product):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " return price\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = [get_valid_price(product) for product in customer_orders]\n", + " return sum(prices)\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " if (quantity - 1 if product in customer_orders else quantity) > 0\n", + " }\n", + " return inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_unique_products_ordered = (\n", + " 0 if len(products) == 0 else (total_products_ordered / len(products)) * 100\n", + " )\n", + " return total_products_ordered, percentage_of_unique_products_ordered\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_of_unique_products_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_of_unique_products_ordered}\")\n", + "\n", + "\n", + "def print_inventory(inventory, title):\n", + " print(title)\n", + " lines = [f\"{product}: {quantity}\" for product, quantity in inventory.items()]\n", + " print(\"\\n\".join(lines))\n", + "\n", + "\n", + "def main():\n", + " inventory = initialize_inventory(products)\n", + " print()\n", + " print_inventory(inventory, \"Initial Inventory:\")\n", + " print()\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + " print()\n", + "\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print()\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " print_inventory(inventory, \"Updated Inventory:\")\n", + " print()\n", + "\n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"Total Price: {total_price}\")\n", + "\n", + "\n", + "main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f485bd8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: ''\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "\n", + "def get_valid_product(inventory):\n", + " while True:\n", + " try:\n", + " product_name = input(\"Enter the name of the product you want to order: \").strip().lower()\n", + "\n", + " if product_name not in inventory:\n", + " raise ValueError(\"This product does not exist in the inventory.\")\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(\"This product is out of stock.\")\n", + "\n", + " return product_name\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = {get_valid_product(inventory) for _ in range(number_of_orders)}\n", + " return customer_orders\n", + "\n", + "\n", + "def get_valid_price(product):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " return price\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = [get_valid_price(product) for product in customer_orders]\n", + " return sum(prices)\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " if (quantity - 1 if product in customer_orders else quantity) > 0\n", + " }\n", + " return inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_unique_products_ordered = (\n", + " 0 if len(products) == 0 else (total_products_ordered / len(products)) * 100\n", + " )\n", + " return total_products_ordered, percentage_of_unique_products_ordered\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_of_unique_products_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_of_unique_products_ordered}\")\n", + "\n", + "\n", + "def print_inventory(inventory, title):\n", + " print(title)\n", + " lines = [f\"{product}: {quantity}\" for product, quantity in inventory.items()]\n", + " print(\"\\n\".join(lines))\n", + "\n", + "\n", + "def main():\n", + " inventory = initialize_inventory(products)\n", + " print()\n", + " print_inventory(inventory, \"Initial Inventory:\")\n", + " print()\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + " print()\n", + "\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print()\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " print_inventory(inventory, \"Updated Inventory:\")\n", + " print()\n", + "\n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"Total Price: {total_price}\")\n", + "\n", + "\n", + "main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "943d0860", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +604,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.0" } }, "nbformat": 4,