From 078894dcd4e7691788ffcccbb6c366a92d497df3 Mon Sep 17 00:00:00 2001 From: Marisa Pagara Date: Mon, 20 Oct 2025 22:10:08 +0100 Subject: [PATCH] solved lab flow control --- lab-python-flow-control.ipynb | 150 +++++++++++++++++++++++++++++++++- 1 file changed, 148 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..d2b4a51 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -10,6 +10,152 @@ "# Lab | Flow Control" ] }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2c8fb83a", + "metadata": {}, + "outputs": [], + "source": [ + "#products list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "08e55667", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "quantity available of each product:\n", + " please enter a non-negative number.\n" + ] + } + ], + "source": [ + "#inventory with user input\n", + "inventory = {}\n", + "print(\"quantity available of each product:\")\n", + "for item in products:\n", + " while True:\n", + " quantity = input(f\" {item}: \").strip()\n", + " if quantity.isdigit(): \n", + " inventory[item] = int(quantity)\n", + " break\n", + " print(\" please enter a non-negative number.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "a363df15", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Add products to the order. Available: t-shirt, mug, hat, book, keychain\n" + ] + } + ], + "source": [ + "# customer orders\n", + "orders = set() \n", + "print(\"Add products to the order. Available:\", \", \".join(products))\n", + "\n", + "while True:\n", + " article = input(\"the product name: \").strip().lower()\n", + " if article in products:\n", + " orders.add(article)\n", + " else:\n", + " print(\"product not available, please select from the list.\")\n", + " break\n", + "\n", + " more = input(\"do you want to add another product? (yes/no): \").strip().lower()\n", + " if more in (\"no\"):\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8840513f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "customer ordered: hat, mug\n" + ] + } + ], + "source": [ + "# show customer order\n", + "print(\"customer ordered:\", \", \".join(sorted(orders)) or \"(none)\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "bff78a4c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "order details:\n", + "total products ordered: 2\n", + "percentage of products ordered: 40.0%\n" + ] + } + ], + "source": [ + "# order details\n", + "total_products_ordered = len(orders)\n", + "percentage_ordered = round((total_products_ordered / len(products)) * 100, 2)\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"order details:\")\n", + "print(f\"total products ordered: {order_status[0]}\")\n", + "print(f\"percentage of products ordered: {order_status[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "78a9642e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "updated inventory after order:\n", + " t-shirt: 1\n", + " mug: 1\n", + " hat: 2\n", + " book: 4\n", + " keychain: 5\n" + ] + } + ], + "source": [ + "# update and print inventory after order\n", + "for item in orders:\n", + " if inventory.get(item, 0) > 0:\n", + " inventory[item] -= 1\n", + "print(\"updated inventory after order:\")\n", + "for item in products:\n", + " print(f\" {item}: {inventory.get(item, 0)}\")" + ] + }, { "cell_type": "markdown", "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", @@ -41,7 +187,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +201,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,