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
168 changes: 165 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,175 @@
"\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": 49,
"id": "1ae3db68-91c5-417d-8a3e-e7b4c205af69",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is the quantity of t-shirts?: 5\n",
"What is the quantity of mugs?: 5\n",
"What is the quantity of hats?: 5\n",
"What is the quantity of books?: 5\n",
"What is the quantity of keychains?: 5\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"What is the quantity of {product}s?: \"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "bacbb87a-096a-4943-827a-5cb8cc619ce2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product you would like to order: t-shirt\n",
"Do you want to add another product? Yes/No: yes\n",
"Enter the name of the product you would like to order: mug\n",
"Do you want to add another product? Yes/No: No\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you!\n"
]
}
],
"source": [
"customer_orders = set()\n",
"\n",
"order = input(\"Enter the name of the product you would like to order: \").lower()\n",
"customer_orders.add(order)\n",
"query = input(\"Do you want to add another product? Yes/No: \").lower()\n",
"\n",
"while query != \"no\":\n",
" if query == \"yes\":\n",
" order = input(\"Enter the name of the product you would like to order: \").lower()\n",
" customer_orders.add(order)\n",
" query = input(\"Do you want to add another product? Yes/No: \").lower()\n",
" else:\n",
" print(\"Invalid Input. Valid options are Yes or No.\")\n",
" query = input(\"Do you want to add another product? Yes/No: \").lower()\n",
"\n",
"print(\"Thank you!\")"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "37ce761a-f2f0-463d-b365-61590dcc7894",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 4, 'hat': 5, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] -= 1\n",
" \n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "fd31744a-288b-49df-b4f8-949dca8d9a6e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is the quantity of t-shirts?: 5\n",
"What is the quantity of mugs?: 5\n",
"What is the quantity of hats?: 5\n",
"What is the quantity of books?: 5\n",
"What is the quantity of keychains?: 5\n",
"Enter the name of the product you would like to order: t-shirt\n",
"Do you want to add another product? Yes/No: yes\n",
"Enter the name of the product you would like to order: mug\n",
"Do you want to add another product? Yes/No: no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you!\n",
"{'t-shirt': 4, 'mug': 4, 'hat': 5, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"What is the quantity of {product}s?: \"))\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"order = input(\"Enter the name of the product you would like to order: \").lower()\n",
"customer_orders.add(order)\n",
"query = input(\"Do you want to add another product? Yes/No: \").lower()\n",
"\n",
"while query != \"no\":\n",
" if query == \"yes\":\n",
" order = input(\"Enter the name of the product you would like to order: \").lower()\n",
" customer_orders.add(order)\n",
" query = input(\"Do you want to add another product? Yes/No: \").lower()\n",
" else:\n",
" print(\"Invalid Input. Valid options are Yes or No.\")\n",
" query = input(\"Do you want to add another product? Yes/No: \").lower()\n",
"\n",
"print(\"Thank you!\")\n",
"\n",
"for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] -= 1\n",
" \n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "394e2777-a6c9-4f13-9dda-a0c674b76b13",
"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 +217,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down