From 1d9dce28dcd6016c2c11ca1075707f9551c3b50d Mon Sep 17 00:00:00 2001 From: Victoria Cano Date: Thu, 19 Feb 2026 16:34:06 +0100 Subject: [PATCH] Updated Lab with Solutions Flow control --- lab-python-flow-control.ipynb | 114 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 3 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..2b91394 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,121 @@ "\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": 26, + "id": "85fe41a6-9d5f-4b9c-ae45-4986175ef6e0", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt 5\n", + "Enter quantity for mug 6\n", + "Enter quantity for hat 7\n", + "Enter quantity for book 4\n", + "Enter quantity for keychain 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 4, 'keychain': 6}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product you would like to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat'}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Would you like to order another product? yes\n", + "What product would you like from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug'}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Would you like to order another product? no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 6, 'book': 4, 'keychain': 6}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "for i in products: \n", + " quantity = int(input(f\"Enter quantity for {i}\"))\n", + " inventory[i] = quantity\n", + "\n", + "print(inventory)\n", + "\n", + "customer_orders = set()\n", + "\n", + "order = input(f\"Enter product you would like to order: \")\n", + "customer_orders.add(order)\n", + "print(customer_orders)\n", + "\n", + "while True:\n", + " extra_order = input(\"Would you like to order another product?\").lower()\n", + " if extra_order == \"yes\":\n", + " product2 = input(f\"What product would you like from {products}\")\n", + " else:\n", + " break\n", + " customer_orders.add(product2)\n", + " print(customer_orders)\n", + "\n", + "\n", + "for item in customer_orders:\n", + " inventory[item] = inventory[item] - 1\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ce55b9d-85b9-41ee-9d69-7fdca37fedc1", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -55,7 +163,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,