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
145 changes: 145 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"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": 1,
"id": "e2292555-20b3-4e8d-b123-a87cef68f85c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventario inicial: {'t-shirt': 50, 'mug': 50, 'hat': 50, 'book': 50, 'keychain': 50}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {\"t-shirt\": 50, \"mug\": 50, \"hat\": 50, \"book\": 50, \"keychain\": 50}\n",
"costumer_orders = set()\n",
"\n",
"print('Inventario inicial:', inventory)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3c134028-4275-4c94-87c6-45256ce9c6ff",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a product: mug\n",
"Would you like to order another product (yes/no): no\n"
]
}
],
"source": [
"adding_products = 'yes' #Inicializa la variable de control\n",
"\n",
"while adding_products == 'yes':\n",
" \n",
" # a. Pide al usuario que ingrese el nombre del producto\n",
" product_name = input('Please enter a product: ')\n",
" \n",
" # b. Añade el producto al conjunto\n",
" costumer_orders.add(product_name) # Llamamos al método .add() con paréntesis\n",
" \n",
" # c. Pregunta si desea añadir otro producto y convierte a minúsculas inmediatamente\n",
" # d. Guarda la respuesta del usuario directamente en la variable de control\n",
" adding_products = input('Would you like to order another product (yes/no): ').lower() "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "85a860e2-41f7-4e7f-9113-c37d14196008",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventario actualizado: {'t-shirt': 50, 'mug': 49, 'hat': 50, 'book': 50, 'keychain': 50}\n"
]
}
],
"source": [
"for product in costumer_orders:\n",
" inventory[product] = inventory[product] - 1\n",
"\n",
"print('Inventario actualizado:', inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72d3bbf3-30b8-4317-a174-59c6dc4ae76a",
"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.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
84 changes: 83 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,88 @@
"\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": 1,
"id": "e2292555-20b3-4e8d-b123-a87cef68f85c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventario inicial: {'t-shirt': 50, 'mug': 50, 'hat': 50, 'book': 50, 'keychain': 50}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {\"t-shirt\": 50, \"mug\": 50, \"hat\": 50, \"book\": 50, \"keychain\": 50}\n",
"costumer_orders = set()\n",
"\n",
"print('Inventario inicial:', inventory)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3c134028-4275-4c94-87c6-45256ce9c6ff",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a product: mug\n",
"Would you like to order another product (yes/no): no\n"
]
}
],
"source": [
"adding_products = 'yes' #Inicializa la variable de control\n",
"\n",
"while adding_products == 'yes':\n",
" \n",
" # a. Pide al usuario que ingrese el nombre del producto\n",
" product_name = input('Please enter a product: ')\n",
" \n",
" # b. Añade el producto al conjunto\n",
" costumer_orders.add(product_name) # Llamamos al método .add() con paréntesis\n",
" \n",
" # c. Pregunta si desea añadir otro producto y convierte a minúsculas inmediatamente\n",
" # d. Guarda la respuesta del usuario directamente en la variable de control\n",
" adding_products = input('Would you like to order another product (yes/no): ').lower() "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "85a860e2-41f7-4e7f-9113-c37d14196008",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventario actualizado: {'t-shirt': 50, 'mug': 49, 'hat': 50, 'book': 50, 'keychain': 50}\n"
]
}
],
"source": [
"for product in costumer_orders:\n",
" inventory[product] = inventory[product] - 1\n",
"\n",
"print('Inventario actualizado:', inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72d3bbf3-30b8-4317-a174-59c6dc4ae76a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +137,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down