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
116 changes: 114 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,123 @@
"\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": null,
"id": "4afd11a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available 89 t-shirt!\n",
"Available 54 mug!\n",
"Available 86 hat!\n",
"Available 57 book!\n",
"Available 33 keychain!\n"
]
}
],
"source": [
"#1. list + specific items\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# dictionary\n",
"inventory = {}\n",
"# quantity\n",
"for product in products:\n",
" quantity = input (f\"Could you, please, let us know what is the quantity available in stock for {product}?\")\n",
" while not quantity.isdigit():\n",
" quantity = input (f\"Wrong format. Please insert a valid quantity for {product}!\")\n",
" print (f\"Available {quantity} {product}!\")\n",
" inventory.update({product: int(quantity)})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83375dea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 89, 'mug': 54, 'hat': 86, 'book': 57, 'keychain': 33}\n",
"The products selected:\n"
]
}
],
"source": [
"# Dictionary verified! \n",
"print (inventory)\n",
"#Empty set\n",
"print (\"The products selected:\")\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3bd3a865",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You chose: book.\n",
"You chose: mug.\n",
"Customer selection:\n",
"{'book', 'mug'}\n"
]
}
],
"source": [
"#2 products wanted\n",
"choice = \"yes\"\n",
"while choice != \"no\":\n",
" preference = input (f\"Could you, please, tell us a product you would like to buy?\")\n",
" if not (preference in products):\n",
" print(f\"Product not available. Please insert a product in {products}!\")\n",
" else:\n",
" customer_orders.add(preference)\n",
" choice = input (f\"Do you want to continue shopping? yes or no?\").lower()\n",
" print (f\"You chose: {preference}.\")\n",
"# Check the set\n",
"print(\"Customer selection:\")\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b04c70e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The quantity of book is 56.\n",
"\n",
"The quantity of mug is 53.\n",
"\n"
]
}
],
"source": [
"#3. Updating customer orders:\n",
"for product in customer_orders:\n",
" inventory.update({product: inventory[product]-1})\n",
" print (f\"The quantity of {product} is {inventory[product]}.\\n\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +167,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down