diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..573684f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,222 @@ "\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": "markdown", + "id": "2d6d41ee", + "metadata": {}, + "source": [ + "## Solution" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33ee11af", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError\n", + " except ValueError:\n", + " print(\"Invalid quantity. Please enter a non-negative whole number.\")\n", + " else:\n", + " inventory[product] = quantity\n", + " break\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f51f9cb", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " product_prices = {}\n", + "\n", + " for product in customer_orders:\n", + " if product not in product_prices:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError\n", + " except ValueError:\n", + " print(\"Invalid price. Please enter a non-negative number.\")\n", + " else:\n", + " product_prices[product] = price\n", + " break\n", + "\n", + " total_price += product_prices[product]\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebc622d0", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = []\n", + " temporary_inventory = inventory.copy()\n", + " inventory_lookup = {product.lower(): product for product in inventory}\n", + "\n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError\n", + " except ValueError:\n", + " print(\"Invalid number of orders. Please enter a non-negative whole number.\")\n", + " else:\n", + " break\n", + "\n", + " for order_number in range(number_of_orders):\n", + " while True:\n", + " try:\n", + " product_input = input(f\"Enter product #{order_number + 1}: \").strip().lower()\n", + "\n", + " if product_input not in inventory_lookup:\n", + " raise ValueError(\"That product is not in the inventory.\")\n", + "\n", + " product = inventory_lookup[product_input]\n", + "\n", + " if temporary_inventory[product] <= 0:\n", + " raise ValueError(\"That product is out of stock.\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error} Please enter a valid product name.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " temporary_inventory[product] -= 1\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce5d4a5d", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " updated_inventory = inventory.copy()\n", + "\n", + " for product in customer_orders:\n", + " if product in updated_inventory and updated_inventory[product] > 0:\n", + " updated_inventory[product] -= 1\n", + "\n", + " return updated_inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " unique_products_ordered = len(set(customer_orders))\n", + " percentage_unique_products_ordered = 0\n", + "\n", + " if products:\n", + " percentage_unique_products_ordered = (unique_products_ordered / len(products)) * 100\n", + "\n", + " return total_products_ordered, percentage_unique_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f7050058", + "metadata": {}, + "outputs": [], + "source": [ + "def run_shop():\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders(inventory)\n", + " total_price = calculate_total_price(customer_orders)\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + " print(\"\\nCustomer orders:\", customer_orders)\n", + " print(\"Total price:\", total_price)\n", + " print(\"Updated inventory:\", updated_inventory)\n", + " print(\"Order statistics:\", order_statistics)\n", + "\n", + " return inventory, customer_orders, total_price, updated_inventory, order_statistics\n", + "\n", + "\n", + "# Run this when you want to test the program manually:\n", + "# run_shop()" + ] + }, + { + "cell_type": "markdown", + "id": "4587366d", + "metadata": {}, + "source": [ + "## Non-interactive check" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8462e3b3", + "metadata": {}, + "outputs": [], + "source": [ + "from unittest.mock import patch\n", + "\n", + "\n", + "test_inputs = iter([\n", + " \"-2\", \"two\", \"2\", \"0\", \"5\", \"1\", \"3\",\n", + " \"abc\", \"-1\", \"4\",\n", + " \"phone\", \"mug\", \"hat\", \"t-shirt\", \"t-shirt\", \"t-shirt\", \"book\",\n", + " \"free\", \"-3\", \"15.50\", \"12\", \"8.25\",\n", + "])\n", + "\n", + "with patch(\"builtins.input\", lambda prompt=\"\": next(test_inputs)):\n", + " sample_inventory = initialize_inventory(products)\n", + " sample_orders = get_customer_orders(sample_inventory)\n", + " sample_total = calculate_total_price(sample_orders)\n", + " sample_updated_inventory = update_inventory(sample_orders, sample_inventory)\n", + " sample_statistics = calculate_order_statistics(sample_orders, products)\n", + "\n", + "assert sample_inventory == {\n", + " \"t-shirt\": 2,\n", + " \"mug\": 0,\n", + " \"hat\": 5,\n", + " \"book\": 1,\n", + " \"keychain\": 3,\n", + "}\n", + "assert sample_orders == [\"hat\", \"t-shirt\", \"t-shirt\", \"book\"]\n", + "assert sample_total == 47.75\n", + "assert sample_updated_inventory == {\n", + " \"t-shirt\": 0,\n", + " \"mug\": 0,\n", + " \"hat\": 4,\n", + " \"book\": 0,\n", + " \"keychain\": 3,\n", + "}\n", + "assert sample_statistics == (4, 60.0)\n", + "\n", + "print(\"All checks passed.\")" + ] } ], "metadata": {