diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..db0e9a0 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -2,17 +2,13 @@ "cells": [ { "cell_type": "markdown", - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "# Lab | Flow Control" ] }, { "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", "metadata": {}, "source": [ "## Exercise: Managing Customer Orders Optimized\n", @@ -37,25 +33,192 @@ "\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": "markdown", + "metadata": {}, + "source": [ + "**1.** `products` list." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Empty `inventory` dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**3.** Ask for the quantity of each product with a `for` loop." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**4.** Empty `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**5.** Instead of a fixed `for _ in range(3)`, use a `while True` loop: ask for a product, add it, then ask \"add another? (yes/no)\" and stop when the answer isn't yes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "while True:\n", + " order = input(\"Enter the name of a product the customer wants to order: \")\n", + " customer_orders.add(order)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " if another.lower().strip() != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**6.** Print the products in `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Products in customer_orders:\", customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**7.** Order statistics stored in the `order_status` tuple." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**8.** Print the order statistics." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**9.** Update the inventory, subtracting 1 only for the products that were actually ordered." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**10.** Print the updated inventory." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.x" } }, "nbformat": 4,