diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..84da072 Binary files /dev/null and b/.DS_Store differ diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..9dc1827 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,160 @@ "\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": 2, + "id": "b893859f-815c-4d51-abd6-c13a825d213d", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "\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 quantity.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + " while True:\n", + " try:\n", + " orders_number = int(input(\"Enter the number of customer orders:\"))\n", + " if orders_number <= 0:\n", + " print(\"Please enter a number above 0.\")\n", + " continue\n", + " except ValueError as error:\n", + " print(f\"{error}! Please enter a valid number!\")\n", + " continue\n", + " \n", + " for orders in range(orders_number):\n", + " while True:\n", + " orders = input(f\"Enter a product name:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " break\n", + " else:\n", + " print(f\"Please choose one of the available products! Here are your options: {products}\")\n", + "\n", + " print(f\"Customer's orders : {customer_orders}\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " return {product: qty for product, qty in inventory.items() if qty > 0}\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 Products Ordered: {percentage_ordered} % \")\n", + "\n", + "def calculate_total_price():\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product} : \"))\n", + " if price < 0:\n", + " print(\"Please enter a number above 0.\")\n", + " else:\n", + " total_price += price\n", + " break\n", + " except ValueError as error:\n", + " print(f\"{error}! Please enter a valid number.\")\n", + " print(f\"Total price:{total_price:.2f} €\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " for products, quantities in inventory.items():\n", + " print(f\"We have {quantities} {products}s!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d601dead-dc2c-4d61-bffa-b6a9ec8d971b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 6\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: 5\n", + "Enter the number of customer orders: 5\n", + "Enter a product name: mug\n", + "Enter a product name: book\n", + "Enter a product name: hat\n", + "Enter a product name: keychain\n", + "Enter a product name: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer's orders : {'mug', 'keychain', 'book', 'hat'}\n", + "Order Statistics:\n", + "Total Products Ordered: 4 \n", + "Percentage of Products Ordered: 80.0 % \n", + "We have 6 t-shirts!\n", + "We have 3 mugs!\n", + "We have 2 hats!\n", + "We have 1 books!\n", + "We have 4 keychains!\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the price of mug : 6\n", + "Enter the price of keychain : 3\n", + "Enter the price of book : 17\n", + "Enter the price of hat : 12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total price:38.00 €\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\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", + "calculate_total_price()" + ] } ], "metadata": { @@ -90,7 +244,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4,