From 3a5d190e75e58a2e6ab659b486840c0c5daa5381 Mon Sep 17 00:00:00 2001 From: JesusB11 Date: Wed, 18 Jun 2025 17:57:19 +0200 Subject: [PATCH] Submiting lab 2 --- lab-python-flow-control.ipynb | 128 +++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..1d3d8ab 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,135 @@ "\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": "b1647d5c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Provide the quantity for t-shirt\n", + "Provide the quantity for mug\n", + "Provide the quantity for hat\n", + "Provide the quantity for book\n", + "Provide the quantity for keychain\n", + "{'book', 'mug', 'hat'}\n", + "Order Statistics:\n", + "Total products ordered: 3\n", + "Percentage of products ordered: 6.0 %\n", + "updated inventory:\n", + "- t-shirt: 10\n", + "- mug: 9\n", + "- hat: 9\n", + "- book: 9\n", + "- keychain: 10\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "quantity_user = \"\"\n", + "questions = 0\n", + "sum_inventory_final = 0\n", + "\n", + "\n", + "while questions < len(products): #just to make sure we are asking for the 5 same products that are in the list.\n", + "\n", + " for product in products: \n", + " print(\"Provide the quantity for\", product) #asking for the imput for each product that goes in the inventory\n", + " quantity_user= input()\n", + " quantity_user_check = quantity_user.isdigit() #checking that it is a number\n", + " if not quantity_user_check or int(quantity_user) <= 0: #if it is not a number or less than 0, we break the loop and print a message\n", + " print(\"Quantity must be a number and greater than 0. Start again\") \n", + " break\n", + " else:\n", + " questions += 1 #if the number is over 0 and the loop continues adding 1 to the questions variable\n", + " # print(questions)\n", + " inventory [product] = int(quantity_user) #adding the product as key and the quantity as value to the dictionary\n", + " sum_inventory_final += int(quantity_user) #adding the quantity to calculate the percentage at the end.\n", + "\n", + "\n", + "status_ordering= True\n", + "customer_orders = set() \n", + "# continue_ordering= (input(\"Do you want to continue ordering? (yes/no): \"))\n", + "\n", + "# if continue_ordering == \"yes\": \n", + "# while continue_ordering == \"yes\":\n", + "# customer_order += (input(\"Enter the name of a prduct that the customer want's to order:\"))\n", + "# customer_orders= set(customer_order) #Adding the data to the set.\n", + "\n", + "while status_ordering == True:\n", + " customer_orders.add (input(\"Enter the name of a prduct that the customer want's to order:\")) \n", + " continue_ordering= (input(\"Do you want to continue ordering? (yes/no): \"))\n", + " if continue_ordering == \"yes\":\n", + " status_ordering = True\n", + " else:\n", + " status_ordering = False\n", + " \n", + "\n", + "print(customer_orders) #printing order as requested in point 6\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "total_products_percentage=(len(customer_orders)/sum_inventory_final*100) \n", + "\n", + "order_status = (total_products_ordered, total_products_percentage) #creating the tuple as requested in point 7\n", + "\n", + "\n", + "print(\"Order Statistics:\") #point 8 printing as requested\n", + "print(\"Total products ordered: \", total_products_ordered) #point 7 statistics 1 done\n", + "print(\"Percentage of products ordered: \", total_products_percentage, \"%\") #point 7 statistics 2 done\n", + "\n", + "for order in customer_orders:\n", + " if order in inventory:\n", + " # print(order)\n", + " inventory[order] = inventory[order] -1 #updating the inventory.\n", + "\n", + "\n", + "\n", + "print(\"updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"- {product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "537e45fb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "yes\n", + "no\n" + ] + } + ], + "source": [ + "Products= [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = set()\n", + "status_orders= True\n", + "\n", + "while status_orders == True:\n", + " order = input(\"Order a product:\")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " new_item=input(\"Do you want to add another item?(Yes/No)\")\n", + " if new_item.lower() ==\"no\":\n", + " break" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +179,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,