diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..0d88f0b 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,184 @@ "\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": 1, + "id": "d73b1fcf-05fa-4ffd-8458-de22310cf176", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many quantities of t-shirts? 4\n", + "How many quantities of mugs? 5\n", + "How many quantities of hats? 6\n", + "How many quantities of books? 7\n", + "How many quantities of keychains? 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': '4', 'mug': '5', 'hat': '6', 'book': '7', 'keychain': '8'}\n" + ] + } + ], + "source": [ + "# Look at your code from the lab data structures, and improve repeated code with loops.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "inventory = {\"t-shirt\": [], \"mug\": [], \"hat\": [], \"book\": [], \"keychain\": []}\n", + "inventory[\"t-shirt\"] = input(\"How many quantities of t-shirts? \")\n", + "inventory[\"mug\"] = input(\"How many quantities of mugs? \")\n", + "inventory[\"hat\"] = input(\"How many quantities of hats? \")\n", + "inventory[\"book\"] = input(\"How many quantities of books? \")\n", + "inventory[\"keychain\"] = input(\"How many quantities of keychains? \")\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2788fa4e-6b97-4ba3-a36e-c9b878096b92", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many quantities of t-shirts? 4\n", + "How many quantities of mugs? 5\n", + "How many quantities of hats? 6\n", + "How many quantities of books? 7\n", + "How many quantities of keychains? 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = input(f\"How many quantities of {product}s? \")\n", + " inventory[product] = int(quantity)\n", + "print(inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "ca398a57-0fe0-4916-9b98-3d02b68e9886", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What is the name of the product you want to order? mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'mug' to your orders.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product to the basket? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for the orders.\n", + "Customer orders: {'mug'}\n", + "The updated inventory: {'t-shirt': 4, 'mug': 4, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "#2a Prompt the user to enter the name of a product that a customer wants to order.\n", + "customer_orders = set()\n", + "while True:\n", + " order = input(\"What is the name of the product you want to order? \").strip().lower()\n", + "#2b Add the product name to the \"customer_orders\" set.\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " print(f\"Added '{order}' to your orders.\")\n", + " else:\n", + " print(f\"'{order}' is not avaible in our products inventory.\")\n", + "#2c Ask the user if they want to add another product (yes/no).\n", + " new_order = input(\"Do you want to add another product to the basket? (yes/no): \").strip().lower()\n", + "# 2d. Continue the loop until the user does not want to add another product.\n", + " if new_order == \"no\":\n", + " print(\"Thank you for the orders.\")\n", + " break\n", + "print (\"Customer orders:\", customer_orders)\n", + "\n", + "# Instead of updating the inventory by subtracting 1 from the quantity of each product, \n", + "#only do it for the products that were ordered (those in \"customer_orders\").\n", + "def new_inventory(inventory, customer_orders):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Cannot reduce inventory for {product}, out of stock.\")\n", + "new_inventory(inventory, customer_orders)\n", + "\n", + "print(\"The updated inventory: \", inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7caede3e-8b8a-47ca-aab8-d45833009ff5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de3b0fdd-27ff-4413-a86b-7d537b186c10", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05bfd26e-02c6-4ee1-bc0c-0163d7773f15", + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea7b1c7f-5852-4a39-874b-5b9f64b21e50", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +233,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,