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
203 changes: 201 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,210 @@
"\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": 61,
"id": "beea36fc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "e3e2b00b",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "c6fadfbd",
"metadata": {},
"outputs": [],
"source": [
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "6b180137",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "markdown",
"id": "61fe8083",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"id": "222ac59b",
"metadata": {},
"source": [
"The customer only can order 3 products: "
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "97592360",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"top\n",
"shoes\n",
"jeans\n"
]
}
],
"source": [
"for i in range(3):\n",
"\n",
" order = input(\"Enter a product: \")\n",
" if order is not inventory:\n",
" customer_orders.add(order)\n",
" print(order)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "ffb48709",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'shoes', 'top', 'jeans'}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "edfd0620",
"metadata": {},
"source": [
"The customer can request the quantity of products it wants, but it must stop the loop:"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "85c71ffc",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "16136934",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 3, 'hat': 2, 'book': 8, 'keychain': 6}\n",
"{'shoes', 'mug', 'hat'}\n"
]
}
],
"source": [
"continue_entering_products = \"yes\"\n",
"\n",
"while continue_entering_products == \"yes\":\n",
" order = input(\"Please, enter a product to order:\")\n",
" \n",
" if order not in inventory:\n",
" customer_orders.add(order)\n",
"\n",
" continue_entering_products = input(\"Do you want to continue ordering a product? (yes/no)\") #The name of the variable should be the same\n",
"\n",
"print(inventory)\n",
"print(customer_orders)\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "4701ee6a",
"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": 66,
"id": "4404559e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 2, 'hat': 1, 'book': 8, 'keychain': 6}\n"
]
}
],
"source": [
"for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -=1\n",
"\n",
"print(inventory)\n",
"\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +254,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down