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
358 changes: 355 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,365 @@
"\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": "62e33e6f-9bc7-4070-8402-4599573666db",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] \n",
"inventory = {}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6bf4e4a8-a875-45fa-a0d6-959ea5d8ad7d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of products available 5\n",
"Please enter the quantity of products available 6\n",
"Please enter the quantity of products available 7\n",
"Please enter the quantity of products available 8\n",
"Please enter the quantity of products available 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The inventory available for each item is: {'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 8, 'keychain': 9}\n"
]
}
],
"source": [
"for item in products: \n",
" quantity = int(input(\"Please enter the quantity of products available\")) \n",
" inventory [item] = quantity \n",
"print (\"The inventory available for each item is:\", inventory)"
]
},
{
"cell_type": "markdown",
"id": "326d45cb-9783-4807-ad7a-4e1f74fb9204",
"metadata": {},
"source": [
"##### "
]
},
{
"cell_type": "markdown",
"id": "e68c3731-8ebd-4fec-820f-9f3eeb4702dc",
"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": "0667e4cc-6c7d-45ad-8811-c6b7073ab586",
"metadata": {},
"source": [
"### CONVERT THE BELOW TO LOOPS "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2a6697eb-7318-4455-beb8-f7c24e6c8cb9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product name mug\n",
"Please enter the product name book\n",
"Please enter the product name hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The customer has ordered the following items: {'hat', 'book', 'mug'}\n"
]
}
],
"source": [
"customer_orders = set () \n",
"\n",
"product_1 = input(\"Please enter the product name\")\n",
"product_2 =input(\"Please enter the product name\")\n",
"product_3 = input(\"Please enter the product name\")\n",
"\n",
"customer_orders.add(product_1)\n",
"customer_orders.add(product_2)\n",
"customer_orders.add(product_3)\n",
"\n",
"print (\"The customer has ordered the following items:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c733c8d-2cba-4e74-8940-eeb30a107e8c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 4,
"id": "89920465-2c3e-4f31-a768-95bb7daf4a00",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product name mug\n",
"Do you want to add another product? (Yes/No) Yes\n",
"Please enter the product name book\n",
"Do you want to add another product? (Yes/No) Yes\n",
"Please enter the product name hat\n",
"Do you want to add another product? (Yes/No) No\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'book', 'mug'}\n"
]
}
],
"source": [
"customer_orders = set () \n",
"\n",
"while True: \n",
" product = input(\"Please enter the product name\")\n",
" customer_orders.add(product) \n",
" add_more = input (\"Do you want to add another product? (Yes/No)\") \n",
" while add_more == \"Yes\": \n",
" product = input(\"Please enter the product name\") \n",
" customer_orders.add(product) \n",
" add_more = input (\"Do you want to add another product? (Yes/No)\") \n",
" \n",
" else: \n",
" break \n",
"print (customer_orders) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5836702c-e336-4c82-b71f-be15f606ab6e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "83096fd5-e39b-4b35-bbe8-8f72b977821e",
"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": 5,
"id": "4664a949-45f7-4a73-a4ad-c4cf4ea9ee94",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'book', 'mug'}\n",
"<class 'dict'>\n",
"<class 'set'>\n"
]
}
],
"source": [
"print (customer_orders)\n",
"print (type (inventory))\n",
"print (type (customer_orders)) "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d50a590b-ae44-4d70-bd6d-62a05e43e9fa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 8, 'keychain': 9}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "777b0431-808e-43e5-bbd5-dae6a3b3c552",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2ee50bc7-4caa-4427-8af8-5d25a9866c7b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 9}\n"
]
}
],
"source": [
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "af813989-1efe-442e-98b6-3df95eb78288",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'keychain'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"item"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "727d3e8e-9059-44f5-a301-2482eafd4dd6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'mug'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"product"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0b1a8830-053e-41d4-951e-d68d32fff7a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'mug'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"product"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ba118de-68b8-499a-9a0b-60ba56a40a3b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4def7c7-9fce-434f-a14c-9f02c326768c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c823899-5599-4ccf-a3c7-5673995afe68",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -55,7 +407,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down