diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..cafece9 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,153 @@ "\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": 18, + "id": "55d62d3f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Quantity cannot be negative.You need to repeat the input.\n", + "Error: invalid literal for int() with base 10: 'mug'\n", + "Error: Number of orders cannot be negative.\n", + "Error: Product not found in inventory. Please enter a valid product name.\n", + "Error: Product already ordered. Please enter a different product.\n", + "Order Statistics:\n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 19\n", + "hat: 29\n", + "book: 40\n", + "keychain: 50\n", + "Error: Price cannot be negative.\n", + "Error: could not convert string to float: 'bat'\n", + "Total price: 3.0\n" + ] + }, + { + "data": { + "text/plain": [ + "3.0" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#previous data\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#step 1 from lab comprehension + ajusted error handling\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} available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.You need to repeat the input.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "\n", + "\n", + "#step 2 from lab comprehension\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " try:\n", + " orders = int(input(\"Enter the number of different products the customer wants to order: \"))\n", + " if orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " continue\n", + " for i in range(orders):\n", + " while True: \n", + " try:\n", + " p = input(\"Enter the name of a product that a customer wants to order:\")\n", + " if p not in inventory:\n", + " raise ValueError(\"Product not found in inventory. Please enter a valid product name.\")\n", + " if p in customer_orders:\n", + " raise ValueError(\"Product already ordered. Please enter a different product.\")\n", + " customer_orders.add(p)\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders\n", + "\n", + "\n", + "#step 3 from lab comprehension\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory_updated = {product: inventory.get(product) - 1 if product in customer_orders else inventory.get(product) for product in inventory}\n", + " inventory_updated = {product: inventory_updated[product] for product in inventory_updated if inventory_updated[product] > 0}\n", + " return inventory_updated\n", + "\n", + "#step 4 from lab comprehension\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = round((total_products_ordered / len(products)) * 100, 1)\n", + " order_statistics = (total_products_ordered, percentage_ordered)\n", + " return order_statistics\n", + "\n", + "#step 5 from lab comprehension\n", + "def print_order_statistics(order_statistics):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of products ordered: {order_statistics[1]:.2f}%\")\n", + " return order_statistics\n", + "\n", + "#step 6 from lab comprehension\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " {print(f\"{product}: {inventory[product]}\") for product in inventory}\n", + "\n", + "#new step 3 from lab comprehension\n", + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " prices[product] = price\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " total_price = sum([prices[product] for product in prices])\n", + " print(f\"Total price: {total_price:.1f}\")\n", + " return total_price\n", + "\n", + "# Main program from lab comprehension\n", + "\n", + "inventory = initialize_inventory(products)#to set up quantities\n", + "customer_orders = get_customer_orders()#to record the order from the customer\n", + "inventory = update_inventory(customer_orders, inventory)#to update the inventory after the order\n", + "order_statistics = calculate_order_statistics(customer_orders, products)#calculate statistics\n", + "print_order_statistics(order_statistics)#to print statistics\n", + "print_updated_inventory(inventory)#to print the updated inventory\n", + "calculate_total_price(customer_orders)#to calculate total price of the order" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +232,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,