diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..48b70b8 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -44,15 +44,145 @@ "execution_count": null, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Quantity cannot be negative.\n", + "Error: invalid literal for int() with base 10: 'mug'\n", + "Error: Product not recognized.\n", + "Processing order...\n", + "Processing order...\n", + "Error: Please answer with 'yes' or 'no'.\n", + "Error: You have already ordered this product.\n", + "Processing order...\n", + "Processing order...\n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 11\n", + "hat: 29\n", + "book: 40\n", + "keychain: 50\n" + ] + } + ], "source": [ - "# your code goes here" + "#previous data\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#step 1\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " p = int(input(f\"Enter the quantity for {product}: \"))\n", + " if p < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = p\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "\n", + "#step 2\n", + "customer_orders = set()\n", + "def get_customer_orders(products):\n", + " while True:\n", + " try:\n", + " p = input(\"Select a product to order:\")\n", + " if p in customer_orders:\n", + " raise ValueError(\"You have already ordered this product.\")\n", + " elif p not in products:\n", + " raise ValueError(\"Product not recognized.\")\n", + " customer_orders.add(p)\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " finally:\n", + " print(\"...loading...\")\n", + " return customer_orders\n", + "\n", + "def repeat_order(customer_orders):\n", + " while True:\n", + " try:\n", + " b = input(\"Do you want to order more? (yes/no): \")\n", + " if b.lower() == \"yes\":\n", + " get_customer_orders(products)\n", + " elif b.lower() == \"no\":\n", + " break\n", + " else:\n", + " raise ValueError(\"Please answer with 'yes' or 'no'.\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders\n", + "\n", + "#step 3\n", + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + " return inventory\n", + "\n", + "#step 4\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = round((total_products_ordered / len(products)) * 100, 1)\n", + " order_statistics = (total_products_ordered, percentage_ordered)\n", + " return order_statistics\n", + "\n", + "#step 5\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"Total products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of products ordered: {order_statistics[1]:.2f}%\")\n", + " return order_statistics\n", + "\n", + "#step 6\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")\n", + "\n", + "# Main program\n", + "\n", + "inventory = initialize_inventory(products)#to set up quantities\n", + "customer_orders = get_customer_orders(products)#to record the order from the customer\n", + "customer_orders = repeat_order(customer_orders)#to allow multiple orders\n", + "inventory = update_inventory(customer_orders, inventory)#to update the inventory after the order\n", + "order_statistics = calculate_order_statistics(customer_orders, products)#calculate statistics\n", + "print_order_statistics(order_statistics)#to print statistics\n", + "print_updated_inventory(inventory)#to print the updated inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9f16ebc9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -66,7 +196,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,