diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..76d3ffd 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,147 @@ "\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": 1, + "id": "606a5213", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Only 3 hat(s) available.\n", + "Only 3 hat(s) available.\n", + "Product not found in inventory. Please try again.\n", + "Product not found in inventory. Please try again.\n", + "Only 4 mug(s) available.\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 1\n", + "hat: 1\n", + "book: 2\n", + "keychain: 1\n", + "Total Price: {'hat': 10.0, 'mug': 5.0}\n" + ] + } + ], + "source": [ + "#1.\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "#3.\n", + "def get_customer_orders(inventory):\n", + " orders = {}\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"How many different products would you like to order? \"))\n", + " if num_orders < 0:\n", + " print(\"Number of orders cannot be negative.\")\n", + " else:\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " for i in range(num_orders):\n", + " while True:\n", + " product_name = input(f\"Enter the name of product #{i + 1}: \")\n", + " if product_name not in inventory:\n", + " print(\"Product not found in inventory. Please try again.\")\n", + " elif inventory[product_name] <= 0:\n", + " print(f\"Sorry, {product_name} is out of stock.\")\n", + " else:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"How many {product_name}s do you want to order? \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative\")\n", + " elif quantity > inventory[product_name]:\n", + " print(f\"Only {inventory[product_name]} {product_name}(s) available.\")\n", + " else:\n", + " orders[product_name] = quantity\n", + " inventory[product_name] -= quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid number.\")\n", + " break\n", + " return orders\n", + "\n", + "#\n", + "def update_inventory(customer_orders, inventory):\n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + " return {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + "\n", + "#\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total = len(customer_orders)\n", + " percent = (total / len(products)) * 100\n", + " return total, round(percent, 2)\n", + "\n", + "def print_order_statistics(stats):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(\"Total Products Ordered:\", stats[0])\n", + " print(\"Percentage of Unique Products Ordered:\", stats[1])\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " [print(f\"{item}: {quantity}\") for item, quantity in inventory.items()]\n", + "\n", + "#2.\n", + "def calculate_total_price(inventory):\n", + " prices = {}\n", + " for product in inventory:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: €\"))\n", + " if price < 0:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " else:\n", + " prices[product] = price\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " return prices\n", + "\n", + "#\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(stats)\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(\"Total Price:\", total_price)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +226,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,