diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..0f248ab 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,77 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9656f70f", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "print(\"Enter initial inventory quantities:\")\n", + "for p in products:\n", + " while True:\n", + " try:\n", + " qty = int(input(f\"Enter the quantity of {p}: \"))\n", + " if qty < 0:\n", + " print(\"Please enter a non-negative number.\")\n", + " continue\n", + " inventory[p] = qty\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + "\n", + "print(\"\\nInventory created:\", inventory)\n", + "\n", + "customer_orders = set()\n", + "print(\"\\nAvailable products:\", products)\n", + "\n", + "while True:\n", + " item = input(\"\\nEnter a product to add to the order: \").strip().lower()\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " print(f\"Added '{item}'. Current order (unique): {customer_orders}\")\n", + " else:\n", + " print(\"Invalid product, please choose from:\", products)\n", + " # don't ask to continue on invalid; loop back to ask for a valid product\n", + " continue\n", + "\n", + " \n", + " another = input(\"Add another product? (yes/no): \").strip().lower()\n", + " if another in (\"no\", \"n\"):\n", + " break\n", + " elif another in (\"yes\", \"y\"):\n", + " continue\n", + " else:\n", + " print(\"Didn't catch that—stopping here.\")\n", + " break\n", + "\n", + "print(\"\\nCustomer order set (unique):\", customer_orders)\n", + "\n", + "total_unique_ordered = len(customer_orders)\n", + "percentage_ordered = (total_unique_ordered / len(products)) * 100 if products else 0.0\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Unique Products Ordered: {total_unique_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "for item in customer_orders:\n", + " inventory[item] = max(0, inventory[item] - 1)\n", + "\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -55,7 +121,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,