diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59c1366 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +.ipynb_checkpoints/ +__pycache__/ +data.pkl diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..0640e21 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,124 @@ "\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": null, + "id": "45e5bcda-221d-4ebb-ab6f-ec01f01322cd", + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Initialize inventory with validation\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\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 number.\")\n", + " return inventory\n", + "\n", + "# 2. Collect and validate customer orders\n", + "def get_customer_orders(inventory):\n", + " valid_num = False\n", + " while not valid_num:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders >= 0:\n", + " valid_num = True\n", + " else:\n", + " print(\"Number of orders cannot be negative.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + " if product not in inventory:\n", + " print(f\"Error: {product} is not in the inventory. Please try again.\")\n", + " elif inventory[product] <= 0:\n", + " print(f\"Error: {product} is out of stock. Please try again.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " return customer_orders\n", + "\n", + "# 3. Calculate total price with validation\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_price = 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 numeric price.\")\n", + " return total_price\n", + "\n", + "# Supporting functions\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " # Remove out-of-stock products\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " return inventory\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_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_ordered:.1f}%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "# Main program flow\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "# Pass the validated inventory to the order function:\n", + "customer_orders = get_customer_orders(inventory) \n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3f85921-19dd-4bf0-96ec-7c62c40454e8", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +208,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,