diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..e77a2bd 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,110 @@ "\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": 11, + "id": "b1c54e5e-af98-4a94-999b-d8fbb9b50480", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 23\n", + "Please enter the quantity of mug: 70\n", + "Please enter the quantity of hat: 15\n", + "Please enter the quantity of book: 34\n", + "Please enter the quantity of keychain: 28\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 23, 'mug': 70, 'hat': 15, 'book': 34, 'keychain': 28}\n" + ] + } + ], + "source": [ + "# 1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "products=['t-shirt','mug','hat','book','keychain']\n", + "inventory={}\n", + "\n", + "for product in products:\n", + " qty=int(input(f'Please enter the quantity of {product}:'))\n", + " inventory[product]=qty\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "430abec4-2d1b-47a0-8969-255f416249bc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product you want to order: mug\n", + "Do you want to order another product? Answer only with 'yes' or 'no': yes\n", + "Enter the name of the product you want to order: keychain\n", + "Do you want to order another product? Answer only with 'yes' or 'no': yes\n", + "Enter the name of the product you want to order: t-shirt\n", + "Do you want to order another product? Answer only with 'yes' or 'no': yes\n", + "Enter the name of the product you want to order: hat\n", + "Do you want to order another product? Answer only with 'yes' or 'no': no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'keychain', 't-shirt', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# 2.\n", + "\n", + "customer_orders=set()\n", + "\n", + "answer='yes'\n", + "while answer=='yes':\n", + " order=input('Enter the name of the product you want to order:')\n", + " customer_orders.add(order)\n", + " answer=input(\"Do you want to order another product? Answer only with 'yes' or 'no':\")\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "6a13c7ac-b9cb-4b13-aba0-affbd6783c8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 22, 'mug': 69, 'hat': 14, 'book': 34, 'keychain': 27}\n" + ] + } + ], + "source": [ + "# 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\").\n", + "\n", + "for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order]-=1\n", + "\n", + "print(inventory)" + ] } ], "metadata": { @@ -55,7 +159,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4,