Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions lab-python-flow-control-solutions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"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": 31,
"id": "3d17cd44",
"metadata": {},
"outputs": [],
"source": [
"products= [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"inventory= {}\n",
"t_shirt=int(input(\"Enter the quantity of t-shirt:\"))\n",
"mug=int(input(\"Enter the quantity of mug:\"))\n",
"hat= int(input(\"Number of hats?\"))\n",
"book= int(input(\"Number of books?\"))\n",
"keychain= int(input(\"Number of keychains?\"))\n",
"inventory['t-shirt']=t_shirt\n",
"inventory['mug']=mug\n",
"inventory['hat']=hat\n",
"inventory['book']=book\n",
"inventory['keychain']=keychain\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d24db976",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d524a6bf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n",
"{'mug'}\n",
"You just added a mug\n",
"You just added a hat\n",
"You just added a book\n",
"No more items added\n",
"{'mug', 'book', 'hat'}\n",
"3\n",
"The percentage is 60.0%\n",
"(60.0,)\n",
"Order Statistics:\n",
"Total Products Ordered:3\n",
"Percentage of Products Ordered:60.0%\n",
"t-shirt 5\n",
"mug 4\n",
"hat 4\n",
"book 4\n",
"keychain 5\n"
]
}
],
"source": [
"print(inventory)\n",
"customer_orders= set()\n",
"my_list=input(\"Which item do you want? t-shirt, mug, hat, book or keychain?\").lower()\n",
"customer_orders.add(my_list)\n",
"print(customer_orders)\n",
"add_order=input(\"Do you want to add another product? (yes/no)\").strip().lower()\n",
"add_order=(add_order==\"yes\")\n",
"while True:\n",
" add_item=input(\"Which item do you want to add?\").lower()\n",
" customer_orders.add(add_item)\n",
" print(f\"You just added a {add_item}\")\n",
" last_add=input (\"Do you want to add something more?(yes/no)\").strip().lower()\n",
" is_true=(last_add==\"yes\")\n",
" #last_add se convierte en bool al hacerlo ==\"yes\"\n",
" if not is_true:\n",
" print(\"No more items added\")\n",
" break\n",
" \n",
"print(customer_orders)\n",
"total_products_ordered=len(customer_orders)\n",
"print(total_products_ordered)\n",
"percentage= len(customer_orders)/len(products)*100\n",
"print( f\"The percentage is {percentage}%\")\n",
"order_status=(percentage,)\n",
"print(order_status)\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered:{total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered:{percentage}%\")\n",
"\n",
"for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item]-=1\n",
"\n",
"for key, value in inventory.items():\n",
" print(key,value)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
63 changes: 0 additions & 63 deletions lab-python-flow-control.ipynb

This file was deleted.