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
139 changes: 136 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,146 @@
"\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": 3,
"id": "7627ade6-b90f-4cdd-8c5b-e0ef5c2dbbe6",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c4bed7c1-4925-477d-8fc9-731f655ea7f6",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b13c38df-ac9d-4876-b774-78b8acab388f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Number of t-shirt: 22\n",
"Number of mug: 25\n",
"Number of hat: 24\n",
"Number of book: 27\n",
"Number of keychain: 22\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 22, 'mug': 25, 'hat': 24, 'book': 27, 'keychain': 22}\n"
]
}
],
"source": [
"for item in products:\n",
" inventory[item] = int(input(\"Number of \" + item + \": \"))\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "15658d90-5d7b-49b3-b959-192b53962115",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set ()\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "afc1f4f5-f6a2-4320-ab7d-3fbe3a75800b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a product: hat\n",
"Do you want something else? (yes/no): yes\n",
"Choose a product: book\n",
"Do you want something else? (yes/no): no\n"
]
}
],
"source": [
"answer = \"yes\"\n",
"while answer == \"yes\":\n",
" product = input(\"Choose a product: \")\n",
" customer_orders.add(product)\n",
" answer = input(\"Do you want something else? (yes/no): \").lower()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "77fdbfa3-0cb4-4ebc-b01a-19f176a00f76",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'book', 'hat'}\n"
]
}
],
"source": [
"print(\"Customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "dc523683-9cbd-4391-a1be-0ffacc25d7ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 22, 'mug': 25, 'hat': 23, 'book': 26, 'keychain': 22}\n"
]
}
],
"source": [
"for i in customer_orders:\n",
" if i in inventory:\n",
" inventory[i] -= 1\n",
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0e3ccd4-47b5-40a1-b5e5-fa83c2d158f8",
"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 +188,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down