diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..bb69588 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,118 @@ "\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": 33, + "id": "307d4fb1", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirts\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products: \n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "def get_customer_order(inventory):\n", + " customers_order = set()\n", + "\n", + " order_input = input(\"Please enter the products you want to order, separated by commas: \")\n", + " orders = [order.strip() for order in order_input.split(\",\")]\n", + "\n", + " for product in orders:\n", + " while True:\n", + " if product not in inventory:\n", + " print(f\"'{product}' does not exist in inventory.\")\n", + " break\n", + " elif inventory[product] <= 0:\n", + " print(f\"'{product}' is out of stock.\")\n", + " break\n", + " else:\n", + " customers_order.add(product)\n", + " break\n", + "\n", + " return customers_order\n", + "\n", + "def calculate_total_price(customers_order):\n", + " total_price = 0\n", + "\n", + " for product in customers_order:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Please enter the price of {product}: \"))\n", + " if price < 0:\n", + " print(\"Price cannot be negative.\")\n", + " else:\n", + " total_price += price\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric value.\")\n", + "\n", + " return total_price\n", + "\n", + "def update_inventory(customers_order, inventory):\n", + " for product in customers_order:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " return inventory\n", + "def calculate_order_statistics(customers_order, products):\n", + " total_ordered = len(customers_order)\n", + " percentage_ordered = total_ordered / len(products) * 100\n", + " return total_ordered, percentage_ordered\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage_ordered = order_statistics\n", + " print(f\"total ordered: {total_ordered}\")\n", + " print(f\"percentage ordered: {percentage_ordered: .2f}%\")\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "74c73c17", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'mug' is out of stock.\n", + "'hate' does not exist in inventory.\n", + "{'book'}\n", + "Total price: 5.0\n", + "(1, 20.0)\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "\n", + "customers_order = get_customer_order(inventory)\n", + "print(customers_order)\n", + "\n", + "total_price = calculate_total_price(customers_order)\n", + "print(\"Total price:\", total_price)\n", + "\n", + "stats = calculate_order_statistics(customers_order, products)\n", + "print(stats)\n", + "\n", + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +197,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,