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
132 changes: 130 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,139 @@
"\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": 25,
"id": "f0ad8e88",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 98, 'mug': 65, 'hat': 32, 'book': 78, 'keychain': 45}\n"
]
}
],
"source": [
"# define products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# create empty dictionary\n",
"inventory = {}\n",
"\n",
"inventory = {product: int(input(f\"Please input the amount of the product {product}\")) for product in products}\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "77f143f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total order:\n",
"keychain\n",
"mug\n",
"book\n"
]
}
],
"source": [
"#make emty set\n",
"customer_orders = set()\n",
"\n",
"#keep asking until no\n",
"while True:\n",
" product = input(\"which product would you like to order?\")\n",
" #add to customer_orders\n",
" customer_orders.add(product)\n",
" #ask something else?\n",
" next_order = input(\"would you like to order something else? Please answer 'yes' or 'no':\").strip().lower() \n",
"\n",
" if next_order == 'no':\n",
" break\n",
"\n",
"#print order\n",
"print(\"Total order:\")\n",
"for order in customer_orders:\n",
" print(order)\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "2c4dbf27",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"Order Statistics:\n",
"Total Products Ordered: 4\n",
"Percentage of Products Ordered: 1.257861635220126%\n"
]
}
],
"source": [
"#establish total number of orders\n",
"tot_products_ordered = len(customer_orders)\n",
"#establish total number of products\n",
"tot_products = sum(inventory.values())\n",
"#calculate percentage\n",
"percentage_products_ordered = (tot_products_ordered / tot_products) * 100\n",
"#create tuple\n",
"order_status = (tot_products_ordered, percentage_products_ordered)\n",
"\n",
"print(type(order_status))\n",
"\n",
"print(f\"Order Statistics:\\n\"\n",
" f\"Total Products Ordered: {tot_products_ordered}\\n\"\n",
" f\"Percentage of Products Ordered: {percentage_products_ordered}%\")"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "ca379b75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 98\n",
"mug: 64\n",
"hat: 32\n",
"book: 77\n",
"keychain: 44\n"
]
}
],
"source": [
"#ordered items in customer_orders\n",
"#substract customer_order of all products\n",
"\n",
"\n",
"updated_inventory = {product: (quantity - 1 if product in customer_orders else quantity) for product, quantity in inventory.items()}\n",
"#print each item on a new line\n",
"print(\"Updated Inventory:\")\n",
"for product, quantity in updated_inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +183,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down