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
158 changes: 158 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"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": 5,
"id": "f2e740d1-5575-4d8e-99c1-e450c17c8cb6",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What´s the quantity of t-shirt? 6\n",
"What´s the quantity of mug? 9\n",
"What´s the quantity of hat? 7\n",
"What´s the quantity of book? 2\n",
"What´s the quantity of keychain? 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"What do you want to order?\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product name: book\n",
"Do you want to order anything else? Type Yes or No. yes\n",
"Enter a product name: mug\n",
"Do you want to order anything else? Type Yes or No. no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your order.\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"customer_orders = set()\n",
"for product in products:\n",
" outcome=input(f\"What´s the quantity of {product}?\")\n",
" inventory[product]=int(outcome)\n",
"\n",
"print(\"What do you want to order?\")\n",
"user_choice2 = \"yes\"\n",
"while user_choice2 == \"yes\":\n",
" user_choice = input(\"Enter a product name: \")\n",
" if user_choice in products:\n",
" customer_orders.add(user_choice)\n",
" else:\n",
" print(\"Please enter valid product from the list.\")\n",
" user_choice2 = input(\"Do you want to order anything else? Type Yes or No.\").strip().lower()\n",
"else: \n",
" print(\"Thank you for your order.\") \n",
" \n",
"for product in customer_orders:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "58387a11-fb71-4ff7-80cc-ff3cf5878caf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 6, 'mug': 8, 'hat': 7, 'book': 1, 'keychain': 1}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "069141b5-c738-4242-81f3-0b6cef0b562d",
"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
}
61 changes: 60 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,65 @@
"\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": "f2e740d1-5575-4d8e-99c1-e450c17c8cb6",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"customer_orders = set()\n",
"for product in products:\n",
" outcome=input(f\"What´s the quantity of {product}?\")\n",
" inventory[product]=int(outcome)\n",
"\n",
"print(\"What do you want to order?\")\n",
"user_choice2 = \"yes\"\n",
"while user_choice2 == \"yes\":\n",
" user_choice = input(\"Enter a product name: \")\n",
" if user_choice in products:\n",
" customer_orders.add(user_choice)\n",
" else:\n",
" print(\"Please enter valid product from the list.\")\n",
" user_choice2 = input(\"Do you want to order anything else? Type Yes or No.\").strip().lower()\n",
"else: \n",
" print(\"Thank you for your order.\") \n",
" \n",
"for product in customer_orders:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "58387a11-fb71-4ff7-80cc-ff3cf5878caf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 6, 'mug': 8, 'hat': 7, 'book': 1, 'keychain': 1}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "069141b5-c738-4242-81f3-0b6cef0b562d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +114,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down