diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..8d7d6f1 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,205 @@ "\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": 3, + "id": "4b213ad5", + "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", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " else:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "46964589", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " num_orders = int(input(\"How many different products does the customer want to order? \"))\n", + " if num_orders < 0:\n", + " print(\"Number of orders cannot be negative. Please enter a valid number.\")\n", + " else:\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", + " for i in range(num_orders):\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " product = input(f\"Enter product {i + 1}: \").lower()\n", + "\n", + " if product not in inventory:\n", + " print(\"This product does not exist. Please enter a valid product.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"This product is out of stock. Please choose another product.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + "\n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "c1e466ef", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ebf1c00a", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + " order_statistics = (total_products_ordered, percentage_products_ordered)\n", + "\n", + " return order_statistics\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "1927d4c7", + "metadata": {}, + "outputs": [], + "source": [ + "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]}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b69982b9", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "4867c682", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_input = False\n", + "\n", + " while not valid_input:\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", + " total_price += price\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "5f047dad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This product does not exist. Please enter a valid product.\n", + "This product does not exist. Please enter a valid product.\n", + "Order Statistics\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n", + "Updated Inventory\n", + "t-shirt: 100\n", + "mug: 49\n", + "hat: 19\n", + "book: 5\n", + "keychain: 1\n", + "Total Price: 130.0\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "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", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "58f2feb1", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +284,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,