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
250 changes: 249 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,254 @@
"\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": 27,
"id": "b5ff3c0f-a709-4dcd-9d2c-152dc1a8b364",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Quantify t-shirt product: 10\n",
"Quantify mug product: 50\n",
"Quantify hat product: 20\n",
"Quantify book product: 50\n",
"Quantify keychain product: 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 50, 'hat': 20, 'book': 50, 'keychain': 20}\n"
]
}
],
"source": [
"#1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory ={\"t-shirt\": 0, \"mug\": 0, \"hat\": 0, \"book\": 0, \"keychain\": 0}\n",
"\n",
"for product in inventory:\n",
" inventory[product] = int(input(f\"Quantify {product} product: \"))\n",
"\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "776c4213-6ffc-4d5b-9e1b-2cd1e8115cad",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add selected product: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product book added to cart\n"
]
}
],
"source": [
"#2a\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_order = []\n",
"\n",
"selected = input(\"Add selected product: \").lower()\n",
"\n",
"\n",
"if selected in products:\n",
" customer_order.append(selected)\n",
" print(f\"Product {selected} added to cart\")\n",
"else:\n",
" print(f\"Product not found\")\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "426cef37-1e3f-4678-b270-8957f0537faa",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add selected product: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product t-shirt added to cart\n"
]
}
],
"source": [
"#2b\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_order = set()\n",
"\n",
"selected = input(\"Add selected product: \").lower()\n",
"\n",
"\n",
"if selected in products:\n",
" customer_order.add(selected)\n",
" print(f\"Product {selected} added to cart\")\n",
"else:\n",
" print(f\"Product not found\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "ca020f45-be18-4b0c-8f1c-e2f7afeef249",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add selected product: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product mug added to cart\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Want other product?: yes\n"
]
}
],
"source": [
"#2c\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_order = set()\n",
"\n",
"selected = input(\"Add selected product: \").lower()\n",
"\n",
"\n",
"if selected in products:\n",
" customer_order.add(selected)\n",
" print(f\"Product {selected} added to cart\")\n",
" other_product = input(\"Want other product?: \").lower\n",
"else:\n",
" print(f\"Product not found\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "5abb2ca1-4b63-45e4-82fe-8262bf76a249",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add selected product: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product book added to cart\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Want other product? (yes/no): yes\n",
"Add selected product: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product t-shirt added to cart\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Want other product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your purchase\n",
"{'t-shirt', 'book'}\n"
]
}
],
"source": [
"# 2d\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_order = set()\n",
"\n",
"other_product = \"yes\"\n",
"\n",
"while other_product == \"yes\":\n",
" selected = input(\"Add selected product: \").lower()\n",
"\n",
" if selected in products:\n",
" customer_order.add(selected)\n",
" print(f\"Product {selected} added to cart\")\n",
" else:\n",
" print(\"Product not found\")\n",
"\n",
" other_product = input(\"Want other product? (yes/no): \").lower()\n",
"\n",
"print(\"Thank you for your purchase\")\n",
"print(customer_order)\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "d38dedf8-a7bb-417f-b3f6-fbb056a3f7ab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 9, 'mug': 50, 'hat': 20, 'book': 49, 'keychain': 20}\n"
]
}
],
"source": [
"for product in customer_order:\n",
" inventory[product] -=1\n",
"print(inventory)"
]
}
],
"metadata": {
Expand All @@ -55,7 +303,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down