From 712f2e55447546bd6bb106dd7ac8ac6deb55b9af Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 19 Feb 2026 15:26:32 +0100 Subject: [PATCH] solved lab --- lab-python-flow-control.ipynb | 132 +++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..68d1a30 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,136 @@ "\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": "a3209076-ad1e-4296-a8f6-8e638d1c7d4a", + "metadata": {}, + "outputs": [], + "source": [ + "#Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + "#a. Prompt the user to enter the name of a product that a customer wants to order.\n", + "#b. Add the product name to the \"customer_orders\" set.\n", + "#c. Ask the user if they want to add another product (yes/no).\n", + "#d. Continue the loop until the user does not want to add another product." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1871d8cf-0219-4e24-be04-d9cdd8541a3f", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c341b2aa-ea9c-4256-8752-3841ac65d920", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c14a6244-3e67-44c6-865b-245a7ba9f9a7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product to order : book\n", + "Do you want to order another product? (Yes/No) yes\n", + "Choose a product to order : hat\n", + "Do you want to orther another product? (Yes/No) no\n" + ] + } + ], + "source": [ + "product = input(\"Choose a product to order : \")\n", + "customer_orders.add(product)\n", + "question = input(\"Do you want to order another product? (Yes/No)\").lower()\n", + "\n", + "while question != \"no\":\n", + " product = input(\"Choose a product to order : \")\n", + " customer_orders.add(product)\n", + " question = input(\"Do you want to orther another product? (Yes/No)\").lower()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "90531453-365d-422f-8b8e-c73137838ffc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat'}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6eed0e1e-1536-418d-80aa-cdbbfc0ae2b2", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'t-shirt': 8, 'mug': 10, 'hat': 12, 'book': 11, 'keychain': 21}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "ccd5a7f6-9db8-4d0f-8c60-6864174738ba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt 8\n", + "mug 10\n", + "hat 11\n", + "book 10\n", + "keychain 21\n" + ] + } + ], + "source": [ + "#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\").\n", + "\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(product, quantity)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f8278270-6174-403f-ac5e-a852a2ecee91", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +185,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,