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
147 changes: 146 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,151 @@
"\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": 16,
"id": "5acf9e38-eda9-438b-8bf6-24cd05fa2ac4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "af7df725-a6e8-4f59-804a-13e9b545888d",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "d6de9423-4f1e-4851-b52b-1a47c89a9c4e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Number of t-shirt: 10\n",
"Number of mug: 10\n",
"Number of hat: 10\n",
"Number of book: 10\n",
"Number of keychain: 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
]
}
],
"source": [
"for product in products:\n",
" value = int(input(f\"Number of {product}:\"))\n",
" inventory[product] = value\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c6b18b88-993e-4dca-bd04-61f286859e9d",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "17daeed1-cbb1-4c5c-b73c-4de77cdce9e2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product: MUG\n",
"Do you want to continue adding products? yes/no: yes\n",
"Enter the name of a product: hat\n",
"Do you want to continue adding products? yes/no: Yes\n",
"Enter the name of a product: Book\n",
"Do you want to continue adding products? yes/no: no\n"
]
}
],
"source": [
"continue_adding = \"yes\"\n",
"\n",
"while continue_adding == \"yes\":\n",
" product = input(\"Enter the name of a product: \") \n",
" product = product.lower()\n",
"\n",
" if product in products: \n",
" customer_orders.add(product)\n",
" else:\n",
" print(\"This product is not available\")\n",
"\n",
" continue_adding = input(\"Do you want to continue adding products? yes/no: \").lower() "
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3824bc19-90d5-4b0a-82aa-5fa35ccf72ab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 8, 'hat': 8, 'book': 8, 'keychain': 10}\n"
]
}
],
"source": [
"for product in customer_orders: \n",
" inventory[product] = inventory[product]-1\n",
"\n",
"print(inventory) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d32cf151-848a-4543-a577-0b1a5ac1ea86",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1846bde0-46f8-4803-98b6-34caeafffd1b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +200,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down