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
102 changes: 100 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "f39aa459",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
"\n",
Expand All @@ -37,11 +43,103 @@
"\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": 38,
"id": "84a878f8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This product is already in your order!\n",
"{'hat', 'mug'}\n",
"{'t-shirt': 10, 'mug': 19, 'hat': 29, 'book': 40, 'keychain': 50}\n",
"Order Statistics:\n",
"Total Products Ordered:2\n",
"Percentage of Products Ordered:40.00%\n",
"Quantity of t-shirt is 10\n",
"Quantity of mug is 19\n",
"Quantity of hat is 29\n",
"Quantity of book is 40\n",
"Quantity of keychain is 50\n"
]
}
],
"source": [
"#step 1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"#step 2\n",
"inventory = {}\n",
"#step 3\n",
"for product in products:\n",
" while True:\n",
" n = input(f\"Please provide the amount of {product}\")\n",
" if n.isdigit() == True:\n",
" inventory[product]= int(n)\n",
" break\n",
" else:\n",
" print(\"Please provide an acceptable number of pieces\")\n",
"\n",
"#print(inventory)\n",
"\n",
"#step 4\n",
"customer_orders = set()\n",
"#print(customer_orders)\n",
"repeat = True\n",
"\n",
"#step 5\n",
"\n",
"\n",
"while True:\n",
" prod = input(\"Please provide a product to order:\")\n",
" if prod not in products:\n",
" prod = input(\"That product is not available!\")\n",
" elif prod in customer_orders:\n",
" print(\"This product is already in your order!\")\n",
" else:\n",
" customer_orders.add(prod)\n",
" inventory[prod] -= 1\n",
" r = input(\"Do you want to order more products? (yes/no)\")\n",
" while r != \"yes\" and r != \"no\":\n",
" r = input(\"Please answer with 'yes' or 'no':\")\n",
" if r.lower() == \"no\":\n",
" break\n",
" elif r.lower() == \"yes\":\n",
" continue\n",
"\n",
"\n",
"#step 9\n",
"\n",
"#step 6\n",
"print(customer_orders)\n",
"print(inventory)\n",
"#step 7\n",
"total_prod = len(customer_orders)\n",
"perc_prod = (len(customer_orders) / len(products)) * 100\n",
"\n",
"#print(total_prod)\n",
"#print(perc_prod)\n",
"\n",
"order_status = (total_prod, perc_prod)\n",
"#type(order_status)\n",
"\n",
"#step 8\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered:{total_prod}\")\n",
"print(f\"Percentage of Products Ordered:{perc_prod:.2f}%\")\n",
"#step 10\n",
"for product in inventory:\n",
" print(f\"Quantity of {product} is {inventory[product]}\")\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +153,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down