diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..85e0f3f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,314 @@ "\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": 30, + "id": "5caa19b0-22c2-401f-a1dc-00507a389a5c", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "a6e619b3-0010-4474-8085-4d143ecd3e7d", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input: # valid_input FALSE\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " print(\"\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "dba8d31d-5c73-4827-904b-54568e6840fb", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " orders = int(input(\"Enter the number of customer orders: \"))\n", + " if orders >= 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"The number of orders cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " \n", + " for _ in range(orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " ordered_product = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + " if ordered_product not in inventory:\n", + " print(f\"{ordered_product} is an invalid product.\")\n", + " elif inventory[ordered_product] == 0:\n", + " print(f\"{ordered_product} doesn't have stock available.\")\n", + " else:\n", + " customer_orders.add(ordered_product)\n", + " valid_product = True\n", + " \n", + " \n", + " except:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + "\n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "28d1690f-e97f-4fd5-8d6b-5a45d2c307f6", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price (customer_orders):\n", + " prices = []\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try: \n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " prices.append(price)\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + " return sum(prices)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "7d26a5a3-9f34-4048-a2b8-365e25943f1b", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + " inventory = {product : inventory[product] for product in inventory if inventory[product] != 0}\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "709df41e-2373-4532-810d-4dff02682048", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics (customer_orders, products):\n", + " n_products=len(customer_orders)\n", + " percentage=n_products/len(products) * 100\n", + " return n_products, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "2b6eaffc-5579-4159-a806-612726b58968", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\" \")\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of unique products ordered: {order_statistics[1]}%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\")\n", + " print(\"Updated inventory: \")\n", + " for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")\n", + "\n", + "def print_total_price(total_price):\n", + " print(\"\")\n", + " print (\"Total price:\", total_price)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "265a8859-9d4c-437a-9676-44f4f7d8bea3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 2\n", + "Enter the quantity of keychains available: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 3\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: bok\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bok is an invalid product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " \n", + "Order Statistics:\n", + "Total products ordered: 3\n", + "Percentage of unique products ordered: 60.0%\n", + "\n", + "Updated inventory: \n", + "t-shirt: 5\n", + "mug: 3\n", + "hat: 3\n", + "book: 1\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 5\n", + "Enter the price of book: 3\n", + "Enter the price of keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total price: 9.0\n" + ] + } + ], + "source": [ + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders(inventory)\n", + "\n", + "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(inventory)\n", + "\n", + "total_order_price=calculate_total_price(customer_orders)\n", + "print_total_price(total_order_price)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa50215e-b5f4-4156-bf4c-d866419f744d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35eb117f-8089-4012-82a1-b345a91b1c9a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a8a0ef3c-7ea8-41e3-94a4-7857fdefd005", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a60bfbb-bd4e-4280-a231-b3aaf6ca1947", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "56013701-cb9b-450f-ba93-6908c2c00f65", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +391,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,