From b02ebd1bfa709f38502447832014507bd7091833 Mon Sep 17 00:00:00 2001 From: elbgross Date: Sat, 30 Aug 2025 14:47:04 +0200 Subject: [PATCH] lab completed --- lab-flow-control_elbgr.ipynb | 151 ++++++++++++++++++++++++++++++++++ lab-python-flow-control.ipynb | 76 ++++++++++++++++- 2 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 lab-flow-control_elbgr.ipynb diff --git a/lab-flow-control_elbgr.ipynb b/lab-flow-control_elbgr.ipynb new file mode 100644 index 0000000..8f04d47 --- /dev/null +++ b/lab-flow-control_elbgr.ipynb @@ -0,0 +1,151 @@ +{ + "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": 9, + "id": "0998adbf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0, 'book': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0, 'book': 5.0, 'keychain': 5.0}\n", + "This product is not available\n", + "hat: 4.0\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory={}\n", + "for product in products:\n", + " quantity=float(input(f\"Please enter the quantity for {product}\"))\n", + " #here we are defining that product is the key on the dictionary and quantity is the value\n", + " inventory[product]=quantity\n", + " print(inventory)\n", + "\n", + "customers_orders=set()\n", + "\n", + "\n", + "while True:\n", + " order_product=input(\"Please choose an item from the product list\")\n", + " if order_product not in products:\n", + " print(\"This product is not available\")\n", + " continue\n", + " if order_product in customers_orders:\n", + " print(\"You already selected this product\")\n", + " continue\n", + " \n", + " customers_orders.add(order_product)\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " another.lower()\n", + " if another!=\"yes\":\n", + " break\n", + "\n", + "#This code is not the best way to do it is better to set a condition to the loop while.\n", + "#You could also do:\n", + "\n", + "\n", + "#another=\"yes\"\n", + "#another.lower()\n", + "#while another==\"yes\":\n", + "# order_product=input(\"Please choose an item from the product list\")\n", + "# if order_product not in products:\n", + "# print(\"This product is not available\")\n", + "# continue\n", + "# if order_product in customers_orders:\n", + "# print(\"You already selected this product\")\n", + "# continue\n", + " \n", + "# customers_orders.add(order_product)\n", + "# another = input(\"Do you want to add another product? (yes/no): \")\n", + "# \n", + " \n", + " \n", + "for product in customers_orders:\n", + " inventory[product]=float(inventory[product])-1\n", + "\n", + "#The following for loop returns all the items on the dictionary and only modify the product that the customer ordered: \n", + "#for product,quantity in inventory.items():\n", + "# print(f\"{product}: {quantity}\")\n", + " \n", + "# I want to print only the product that the customer ordered alongside the modified quantity. How can i do that?\n", + "for product in customers_orders:\n", + " print(f\"{product}: {inventory[product]}\")\n", + "\n", + " \n", + " \n", + " \n", + " \n", + "\n", + " \n", + " \n", + " \n" + ] + } + ], + "metadata": { + "kernelspec": { + "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.13.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..ec8d6bb 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,83 @@ "\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": null, + "id": "0998adbf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0, 'book': 5.0}\n", + "{'t-shirt': 5.0, 'mug': 5.0, 'hat': 5.0, 'book': 5.0, 'keychain': 5.0}\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'set' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 30\u001b[39m\n\u001b[32m 27\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m customers_orders:\n\u001b[32m 28\u001b[39m inventory[product]=\u001b[38;5;28mfloat\u001b[39m(inventory[product])-\u001b[32m1\u001b[39m\n\u001b[32m---> \u001b[39m\u001b[32m30\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product, quantity \u001b[38;5;129;01min\u001b[39;00m \u001b[43mcustomers_orders\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[32m 31\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mproduct\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquantity\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n", + "\u001b[31mTypeError\u001b[39m: 'set' object is not callable" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory={}\n", + "for product in products:\n", + " quantity=float(input(f\"Please enter the quantity for {product}\"))\n", + " #here we are defining that product is the key on the dictionary and quantity is the value\n", + " inventory[product]=quantity\n", + " print(inventory)\n", + "\n", + "customers_orders=set()\n", + "\n", + "while True:\n", + " order_product=input(\"Please choose an item from the product list\")\n", + " if order_product not in products:\n", + " print(\"This product is not available\")\n", + " continue\n", + " if order_product in customers_orders:\n", + " print(\"You already selected this product\")\n", + " continue\n", + " \n", + " customers_orders.add(order_product)\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " another.lower()\n", + " if another != \"yes\":\n", + " break\n", + " \n", + "\n", + "for product in customers_orders:\n", + " inventory[product]=float(inventory[product])-1\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + " \n", + " \n", + " \n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -55,7 +127,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.6" } }, "nbformat": 4,