Skip to content
Open
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
97 changes: 86 additions & 11 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,108 @@
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
"## Exercise: Optimized customer order management\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",
"In the last lab, I was opening an online store that sells various products. To ensure smooth operations, it 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",
"He did this without using flow control. Let's go one 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",
"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",
"2. Instead of asking the user to enter 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",
" a. Prompt the user to enter the name of a product 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",
" do. 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",
" d. Continue the cycle until the user does not wish 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\")."
"3. Instead of updating inventory by subtracting 1 from the quantity of each product, do it only for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "9e26ac35",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"for p in products:\n",
" inventory[p] = int(input(f\"que cantidad de producto hay?\"))\n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "12d9cd05",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"customer_orders = set()\n",
"añadir_producto = \"yes\"\n",
"\n",
"while añadir_producto == \"yes\":\n",
" producto = input(\"ingrese una opcion de entre : t-shirt, mug, hat, book, or keychain\").lower()\n",
" customer_orders.add(producto)\n",
" añadir_producto = input(\"quiere añadir otro producto? (yes/no):\").lower()\n",
" \n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "e46ead16",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug': 1, 'hat': 2, 'book': 3}\n"
]
}
],
"source": [
"new_inventory = {}\n",
"\n",
"for x in customer_orders:\n",
" if x in inventory:\n",
" inventory[x] -= 1\n",
" new_inventory[x] = inventory[x]\n",
"\n",
"\n",
"print(new_inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +130,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down