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
245 changes: 245 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
{
"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": "markdown",
"id": "272bc537-af6d-49d3-bb50-47be8e3e1043",
"metadata": {},
"source": [
"1. Look at your code from the lab data structures, and improve repeated code with loops."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f3f55a2f-4044-420c-8e57-8f93c9e54cf3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Place your order: keychain\n",
"Place your order: book\n",
"Place your order: pen\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product is not in the list\n"
]
}
],
"source": [
"customer_orders = set()\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"for product in range(3):\n",
" order = str(input(\"Place your order: \"))\n",
"\n",
" if order in products: \n",
" customer_orders.add(order) #.add when we're talking about set's\n",
" else:\n",
" print(\"Product is not in the list\")"
]
},
{
"cell_type": "markdown",
"id": "56492a7c-eda5-4489-a978-1a16024da3bf",
"metadata": {},
"source": [
"2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a6736be1-2576-4c86-abba-a9e38653d405",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Place a order: mug\n",
"Do you want to enter place another order? (yes/no): yes\n",
"Place a order: book\n",
"Do you want to enter place another order? (yes/no): yes\n",
"Place a order: t-shirt\n",
"Do you want to enter place another order? (yes/no): no\n"
]
}
],
"source": [
"customer_orders = set()\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"asking_order = \"yes\"\n",
"\n",
"while asking_order == \"yes\":\n",
"\n",
" order = (input(\"Place a order: \"))\n",
" customer_orders.add(order)\n",
"\n",
" asking_order = input(\"Do you want to enter place another order? (yes/no): \").lower()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bcfe2352-fb8b-4875-9431-35c211a6890f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 't-shirt', 'mug'}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "cb5fd533-72b7-4702-a239-a8339b789ce8",
"metadata": {},
"source": [
"# notas: \n",
"-> .add utilizado para sets, que não pode tomar type str\n",
"\n",
"-> .append used for lists\n",
"\n",
"-> no need to break because when user answer \"no\", the while loop is automatically stopped"
]
},
{
"cell_type": "markdown",
"id": "be0050c0-c017-4eee-a22f-926632a68093",
"metadata": {},
"source": [
"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": 11,
"id": "14e716fa-aa03-4acf-bee6-be5eff8d4176",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The quantity of t-shirt is 122\n",
"The quantity of mug is 357\n",
"The quantity of hat is 903\n",
"The quantity of book is 528\n",
"The quantity of keychain is 507\n"
]
}
],
"source": [
"## previous code:\n",
"inventory = {'t-shirt': 123, 'mug': 358, 'hat': 904, 'book': 529, 'keychain': 508}\n",
"\n",
"for product in products:\n",
" inventory[product] = inventory[product] - 1\n",
" print(f\"The quantity of {product} is {inventory[product]}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "cd351c89-1431-4cfe-900e-63cd5cb5cad5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The quantity of book is 527\n",
"The quantity of t-shirt is 121\n",
"The quantity of mug is 356\n"
]
}
],
"source": [
"for order in customer_orders:\n",
" inventory[order] = inventory[order] - 1\n",
" print(f\"The quantity of {order} is {inventory[order]}\")"
]
},
{
"cell_type": "markdown",
"id": "2f9bc811-8f19-49b3-95e3-bf20c70915dc",
"metadata": {},
"source": [
"# notas:\n",
"-> a set doesnt have keys or indexes."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:anaconda3]",
"language": "python",
"name": "conda-env-anaconda3-py"
},
"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
}
Loading