From 11d15bee54bccf209308d31fb3036e8dad64e2e9 Mon Sep 17 00:00:00 2001 From: Ricardo Higginson Date: Sat, 16 May 2026 16:44:27 +0200 Subject: [PATCH] Lab Solved --- lab-python-flow-control.ipynb | 103 +++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..789473e 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +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": 6, + "id": "7cdddcab", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "70bd53b3", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ac47ae96", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 5, 'keychain': 4}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "for product in products:\n", + " quantity = int(input(\"how many \" + product + \" are there?\"))\n", + " inventory[product] = quantity\n", + "inventory " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c3d3d0b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt'}" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "customer_orders= set ()\n", + "question = input(\"Would you like to purchase an item (Y/N)?\")\n", + "while question.upper() == 'Y':\n", + " item = input(\"Which item would you like to order? (t-shirt, mug, hat, book, keychain) \")\n", + " if item.lower() in inventory:\n", + " customer_orders.add(item.lower())\n", + " else:\n", + " print(\"We don't sell that item\")\n", + " question.upper() = input(\"Would you like to purchase another item? \")\n", + "customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "467be70e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 7, 'book': 5, 'keychain': 4}" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "for item in customer_orders:\n", + " inventory[item] = inventory[item] -1\n", + "inventory" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -55,7 +154,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,