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..395d63e --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,133 @@ +{ + "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": "5406bd24-7514-40a0-80e9-005b9ee6e8b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please enter the quantity of each product available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 5\n", + "mug: 7\n", + "hat: 9\n", + "book: 1\n", + "keychain: 0\n", + "which product do you want to buy: book\n", + "do you want to buy another product? (y/n): y\n", + "which product do you want to buy: book\n", + "do you want to buy another product? (y/n): n\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 7, 'hat': 9, 'book': 0, 'keychain': 0}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory={}\n", + "print(\"please enter the quantity of each product available.\")\n", + "for i in products:\n", + " quantity=int(input(i+\":\"))\n", + " while quantity<0:\n", + " quantity=int(input(\"please enter a new number that is not under 0:\"))\n", + " inventory[i]=quantity\n", + "inventory\n", + "\n", + "customer_orders=set()\n", + "x=\"y\"\n", + "while x==\"y\":\n", + " customer_orders.add(input(\"which product do you want to buy:\"))\n", + " x=input(\"do you want to buy another product? (y/n):\")\n", + "\n", + "for i in customer_orders:\n", + " if inventory[i]!= 0:\n", + " inventory[i]-=1\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65c4b5f4-efce-4f37-8bca-3b6fff33afb8", + "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.10.14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..395d63e 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,76 @@ "\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": "5406bd24-7514-40a0-80e9-005b9ee6e8b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please enter the quantity of each product available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 5\n", + "mug: 7\n", + "hat: 9\n", + "book: 1\n", + "keychain: 0\n", + "which product do you want to buy: book\n", + "do you want to buy another product? (y/n): y\n", + "which product do you want to buy: book\n", + "do you want to buy another product? (y/n): n\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 7, 'hat': 9, 'book': 0, 'keychain': 0}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory={}\n", + "print(\"please enter the quantity of each product available.\")\n", + "for i in products:\n", + " quantity=int(input(i+\":\"))\n", + " while quantity<0:\n", + " quantity=int(input(\"please enter a new number that is not under 0:\"))\n", + " inventory[i]=quantity\n", + "inventory\n", + "\n", + "customer_orders=set()\n", + "x=\"y\"\n", + "while x==\"y\":\n", + " customer_orders.add(input(\"which product do you want to buy:\"))\n", + " x=input(\"do you want to buy another product? (y/n):\")\n", + "\n", + "for i in customer_orders:\n", + " if inventory[i]!= 0:\n", + " inventory[i]-=1\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65c4b5f4-efce-4f37-8bca-3b6fff33afb8", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +125,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.14" } }, "nbformat": 4,