diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..80b82dd --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\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": 2, + "id": "ea5d9032-7387-4f92-9331-6a7592de1470", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many t-shirt would you like to put in your basket? : 1\n", + "How many mug would you like to put in your basket? : 1\n", + "How many hat would you like to put in your basket? : 1\n", + "How many book would you like to put in your basket? : 1\n", + "How many keychain would you like to put in your basket? : 1\n" + ] + } + ], + "source": [ + "#1.Look at your code from the lab data structures, and improve repeated code with loops.\n", + "inventory = {\"t-shirt\":10,\"mug\":10,\"hat\":10,\"book\":10,\"keychain\":10}\n", + "for product in inventory :\n", + " quantity = int (input (f\"How many {product} would you like to put in your basket? : \")) " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b6dd6da0-5ba8-4990-8411-41ac8ae757d5", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Which product would you like to checkout? (separated by comma): T-shirt, Mug, Book, Keychain, Hat mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mugwas added to your order\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Would you like to checkout another product? yes/no no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you. We've received your order, {'mug'}!\n" + ] + } + ], + "source": [ + "#2 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 anoother product\n", + "customer_orders = set ()\n", + "while True :\n", + " product = input (\"Which product would you like to checkout? (separated by comma): T-shirt, Mug, Book, Keychain, Hat\").lower()\n", + "\n", + " if product in inventory:\n", + " customer_orders.add(product)\n", + " print (f\"{product}was added to your order\")\n", + " \n", + " else:\n", + " print (f\"Sorry, {product} is not available\")\n", + "\n", + " customer_input = input(f\"Would you like to checkout another product? yes/no\").lower()\n", + " \n", + " if customer_input == \"no\":\n", + " print (f\"Thank you. We've received your order, {customer_orders}!\")\n", + " break " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "23096fa4-5f80-49de-80ae-fbed196eaec6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 9, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "#3Instead 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", + "for product_key in customer_orders:\n", + " if product_key in inventory and inventory[product_key] > 0:\n", + " inventory[product_key] -= 1\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a18f511-ee30-4337-bd28-3030af00e0c5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..80b82dd 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,120 @@ "\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": 2, + "id": "ea5d9032-7387-4f92-9331-6a7592de1470", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many t-shirt would you like to put in your basket? : 1\n", + "How many mug would you like to put in your basket? : 1\n", + "How many hat would you like to put in your basket? : 1\n", + "How many book would you like to put in your basket? : 1\n", + "How many keychain would you like to put in your basket? : 1\n" + ] + } + ], + "source": [ + "#1.Look at your code from the lab data structures, and improve repeated code with loops.\n", + "inventory = {\"t-shirt\":10,\"mug\":10,\"hat\":10,\"book\":10,\"keychain\":10}\n", + "for product in inventory :\n", + " quantity = int (input (f\"How many {product} would you like to put in your basket? : \")) " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b6dd6da0-5ba8-4990-8411-41ac8ae757d5", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Which product would you like to checkout? (separated by comma): T-shirt, Mug, Book, Keychain, Hat mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mugwas added to your order\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Would you like to checkout another product? yes/no no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you. We've received your order, {'mug'}!\n" + ] + } + ], + "source": [ + "#2 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 anoother product\n", + "customer_orders = set ()\n", + "while True :\n", + " product = input (\"Which product would you like to checkout? (separated by comma): T-shirt, Mug, Book, Keychain, Hat\").lower()\n", + "\n", + " if product in inventory:\n", + " customer_orders.add(product)\n", + " print (f\"{product}was added to your order\")\n", + " \n", + " else:\n", + " print (f\"Sorry, {product} is not available\")\n", + "\n", + " customer_input = input(f\"Would you like to checkout another product? yes/no\").lower()\n", + " \n", + " if customer_input == \"no\":\n", + " print (f\"Thank you. We've received your order, {customer_orders}!\")\n", + " break " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "23096fa4-5f80-49de-80ae-fbed196eaec6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 9, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "#3Instead 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", + "for product_key in customer_orders:\n", + " if product_key in inventory and inventory[product_key] > 0:\n", + " inventory[product_key] -= 1\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a18f511-ee30-4337-bd28-3030af00e0c5", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +169,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,