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
125 changes: 122 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"# Lab | Flow Control"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "058a2b3c-6af4-474c-8e84-cd594c6c4bc4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
Expand Down Expand Up @@ -37,13 +45,124 @@
"\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": "c4161b72-095c-4d58-8099-efd89eca508e",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"#First Loop:\n",
"\n",
"for product in products: #Looping through the products list for every item\n",
" number_of_items = int(input(f\"How many {product} are there in the inventory?\")) #storing the number of items inserted by the user into a variable\n",
" inventory[product] = number_of_items #assigning each number inserted to the respective values of each key (products)\n",
"\n",
"print(inventory)\n",
"\n",
"\n",
"#Second Loop:\n",
"\n",
"customer_orders = set()\n",
"\n",
"product_count = 0 #my product count starts at 0\n",
"\n",
"while product_count < 3: #using a while loop to iterate maximum 3 times (for every product)\n",
" order = str(input(\"What product does your customer want to order from the available ones?\").strip().lower()) #storing the user's inputs into a variable and handling case-sensitive\n",
" if order in products: #as long as the product is in the list, it will be added to the set\n",
" customer_orders.add(order) #adding each input to the set\n",
" product_count += 1 #for each iteration, the product count increases by 1\n",
" else: #in case the product inserted is not in the list\n",
" print(\"Invalid product, choose another one\")\n",
"print(customer_orders)\n",
"\n",
"#Third Loop:\n",
"\n",
"#applying the same logic as above\n",
"\n",
"for product in products:\n",
" inventory[product] = int(inventory[product] - 1)\n",
"print(inventory)\n",
"\n",
"#Fourth Loop:\n",
"\n",
"#applying the same logic as above\n",
"\n",
"for product in products:\n",
" print(\"Updated Inventory:\\n\"\n",
" f\"{product}: {inventory[product]}\\n\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93b569f0-949c-4ac9-81ce-658b3062b652",
"metadata": {},
"outputs": [],
"source": [
"#one product at a time\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"customer_orders = set()\n",
"\n",
"answer = \"yes\"\n",
"\n",
"while answer == \"yes\":\n",
" order = str(input(\"What product does your customer wants to order from the available ones?\").strip().lower())\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Sorry, it's an invalid item\")\n",
"\n",
" answer = input(\"Does the customer want to order anything else? Type yes or no:\").strip().lower()\n",
"\n",
"print(f\"This is your final order: {customer_orders}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffc217a7-e3e8-4f22-b621-a8b754d16e9f",
"metadata": {},
"outputs": [],
"source": [
"#Subtracting 1 from values, just if the product is in customer_orders\n",
"\n",
"inventory = {}\n",
"\n",
"\n",
"for product in products:\n",
" number_of_items = int(input(f\"How many {product} are there in the inventory?\"))\n",
" inventory[product] = number_of_items\n",
"\n",
"print(inventory)\n",
"\n",
"for product in products:\n",
" if product in customer_orders:\n",
" inventory[product] = int(inventory[product] - 1)\n",
"print(inventory)\n",
"\n",
"#updated inventory\n",
"\n",
"for product in products:\n",
" print(\"Updated Inventory:\\n\"\n",
" f\"{product}: {inventory[product]}\\n\"\n",
" )"
]
}
],
"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 +174,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down