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
323 changes: 320 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,330 @@
"\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": "a7c26b62-e619-47fa-9c12-c7d315862147",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter 3 products from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n",
"enter 3 products from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] hat\n",
"enter 3 products from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"the customer orders are:,{'mug', 'hat', 'book'}\n",
"{'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"#1\n",
"\n",
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_orders = set() #empty set to be populated\n",
"for i in range(3): \n",
" product_to_order = input(f'enter 3 products from {products}')\n",
" if product_to_order in products:\n",
" customer_orders.add(product_to_order)\n",
" else:\n",
" print(f'invalid')\n",
"print(f\"the customer orders are:,{customer_orders}\")\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5029c34d-180a-40e0-9112-4ce30230579c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a product you woud like to order chair\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'hat', 'book', 'chair'}\n"
]
}
],
"source": [
"#2a\n",
"customer_order = {'mug', 'book', 'hat'}\n",
"#2b\n",
"desired_product = input(f\"enter a product you woud like to order\")\n",
"customer_order.add(desired_product)\n",
"print(customer_order)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b06306aa-d10e-4cc8-86d0-c8b48ac11b4c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a product you would like to order chair\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'hat', 'book', 'chair'}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"do you want to add another product (yes/no)? yes\n",
"enter a product you woud like to order cap\n",
"do you want to add another product (yes/no)? yes\n",
"enter a product you woud like to order glass\n",
"do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"user doesn't want to add any more product\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"user doesn't want to add any more product\n"
]
}
],
"source": [
"#2c\n",
"customer_order = {'mug', 'book', 'hat'}\n",
"\n",
"desired_product = input(f\"enter a product you would like to order\")\n",
"customer_order.add(desired_product)\n",
"print(customer_order)\n",
"\n",
"keep_going = True #must be initialized before it can control a loop , initialized as True before the loop begins.\n",
"\n",
"while keep_going:#condition to run the loop\n",
" extra_product = input(f\"do you want to add another product (yes/no)?\")\n",
" if extra_product == \"yes\":\n",
" desired_product = input(f\"enter a product you woud like to order\")\n",
" elif extra_product == \"no\":\n",
" print(f\"user doesn't want to add any more product\")\n",
" else:\n",
" print(\"Invalid responde\")\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f18de24e-cac9-432c-856f-76a68f7ed752",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a product you would like to order chair\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'chair', 'book', 'mug', 'hat'}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"do you want to add another product (yes/no)? yes\n",
"enter a product you woud like to order glass\n",
"do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"user doesn't want to add any more product\n"
]
}
],
"source": [
"#2d\n",
"\n",
"customer_order = {'mug', 'book', 'hat'}\n",
"\n",
"desired_product = input(f\"enter a product you would like to order\")\n",
"customer_order.add(desired_product)\n",
"print(customer_order)\n",
"\n",
"keep_going = True #must be initialized before it can control a loop , initialized as True before the loop begins.\n",
"\n",
"while keep_going:#condition to run the loop\n",
" extra_product = input(f\"do you want to add another product (yes/no)?\")\n",
" if extra_product == \"yes\":\n",
" desired_product = input(f\"enter a product you woud like to order\")\n",
" elif extra_product == \"no\":\n",
" keep_going = False # set to false to stop the loop\n",
" print(f\"user doesn't want to add any more product\")\n",
" else:\n",
" print(\"Invalid responde\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "febdfb8e-a4ed-485e-88cf-51cb799e37fe",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter the qty of each ['t-shirt', 'mug', 'hat', 'book', 'keychain'] 5\n",
"enter the qty of each ['t-shirt', 'mug', 'hat', 'book', 'keychain'] 3\n",
"enter the qty of each ['t-shirt', 'mug', 'hat', 'book', 'keychain'] 5\n",
"enter the qty of each ['t-shirt', 'mug', 'hat', 'book', 'keychain'] 6\n",
"enter the qty of each ['t-shirt', 'mug', 'hat', 'book', 'keychain'] 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"current inventory is: {'t-shirt': 5, 'mug': 3, 'hat': 5, 'book': 6, 'keychain': 7}\n",
"current inventory is: 7\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a product you would like to order keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'mug', 'keychain', 'hat'}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"do you want to add another product (yes/no)? yes\n",
"enter a product you woud like to order mug\n",
"do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"user doesn't want to add any more product\n",
"the reduced customer order is: {'t-shirt': 5, 'mug': 2, 'hat': 4, 'book': 5, 'keychain': 6}\n"
]
}
],
"source": [
"#3\n",
"# gather qty for all inventory \n",
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory={ } # gather elements into a dictionary \n",
"for product in products: #iteration - loop\n",
" qty = int(input(f\"enter the qty of each {products}\"))\n",
" inventory[product]= qty\n",
"print('current inventory is:',inventory)\n",
"print('current inventory is:',inventory[product])\n",
"\n",
"#initialize customer order\n",
"customer_order = {'mug', 'book', 'hat'}\n",
"\n",
"#first order entry\n",
"desired_product = input(f\"enter a product you would like to order\")\n",
"customer_order.add(desired_product)\n",
"print(customer_order)\n",
"\n",
"keep_going = True #must be initialized before it can control a loop , initialized as True before the loop begins.\n",
"\n",
"#loop to add more products\n",
"\n",
"while keep_going:#condition to run the loop\n",
" extra_product = input(f\"do you want to add another product (yes/no)?\")\n",
" if extra_product == \"yes\":\n",
" desired_product = input(f\"enter a product you woud like to order\")\n",
" elif extra_product == \"no\":\n",
" keep_going = False # set to false to stop the loop\n",
" print(f\"user doesn't want to add any more product\")\n",
" else:\n",
" print(\"Invalid responde\")\n",
" \n",
"#subtract 1 only from customer ordered products\n",
"\n",
"for product in customer_order:\n",
" inventory[product] -= 1\n",
"print (\"the reduced customer order is:\",inventory)\n",
" \n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70269ece-fae6-4ae0-97f2-2ab7e5bf5281",
"metadata": {},
"outputs": [],
"source": [
"\n",
" "
]
}
],
"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 +372,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down