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
135 changes: 133 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,142 @@
"\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": 11,
"id": "de2d7748",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "794d1576",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Get quantities for inventory\n",
"for item in products:\n",
" quantity = int(input(f\"How many {item} do you want? \"))\n",
" inventory[item] = quantity\n",
"\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "6f893312",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'mug'}\n"
]
}
],
"source": [
"#Get customer orders with a while loop instead of range\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" product = input(\"Which product would you like to order? \")\n",
"\n",
" if product not in products:\n",
" print(f\"Sorry, '{product}' is not a valid product. Please choose from: {products}\")\n",
" else:\n",
" customer_orders.add(product)\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no) \")\n",
" if another.lower() != \"yes\":\n",
" break\n",
"\n",
"print(customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "d3bcc0e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n"
]
}
],
"source": [
"#Order statistics\n",
"total_prod_ordered = len(customer_orders)\n",
"total_prod_available = len(products)\n",
"percentage_prod_ordered = (total_prod_ordered / total_prod_available) * 100\n",
"\n",
"order_status = (total_prod_ordered, percentage_prod_ordered)\n",
"\n",
"print(\n",
" f\"Order Statistics:\\n\"\n",
" f\"Total Products Ordered: {order_status[0]}\\n\"\n",
" f\"Percentage of Products Ordered: {order_status[1]}%\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "72eb65db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The updated inventory is: \n",
"t-shirt 1\n",
"mug 1\n",
"hat 2\n",
"book 4\n",
"keychain 5\n"
]
}
],
"source": [
"\n",
"#Update inventory only for ordered products\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"The updated inventory is: \")\n",
"for item in inventory:\n",
" print(item, inventory[item])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +186,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down