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
100 changes: 98 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,107 @@
"\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": 2,
"id": "0da27de0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"'mu' is not in our inventory. Please select another item.\n",
"'mu' is not in our inventory. Please select another item.\n",
"The customer has ordered {'mug', 'book'}\n",
"\n",
"Order Statistics:\n",
" Total Products Ordered: 2\n",
" Percentage of Products Ordered : 40.0%\n",
"\n",
"The actual quantity of item t-shirt is: 1\n",
"The actual quantity of item mug is: 0\n",
"The actual quantity of item hat is: 1\n",
"The actual quantity of item book is: 0\n",
"The actual quantity of item keychain is: 1\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# print (\"Type 'Exit' at any time to stop purchasing\")\n",
"print (\"Available products\", products)\n",
"\n",
"inventory = {}\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity of {product}: \").strip())\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" order = input('Enter the name of the product to purchase: ').strip().lower()\n",
" \n",
" if order in products:\n",
" customer_orders.add(order)\n",
" \n",
" else:\n",
" while order not in products:\n",
" print (f\"'{order}' is not in our inventory. Please select another item.\")\n",
" order = input('Enter the name of the product to purchase: ').strip().lower()\n",
" if order in products:\n",
" customer_orders.add(order)\n",
"\n",
" validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n",
"\n",
" while validation not in (\"no\", \"yes\"):\n",
" print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n",
" validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n",
"\n",
" if validation == \"no\":\n",
" break\n",
" \n",
"\n",
"print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered/len(products))*100\n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"print (\"Order Statistics:\" \"\\n\", \n",
"f\"Total Products Ordered: {order_status[0]}\" \"\\n\",\n",
"f\"Percentage of Products Ordered : {order_status[1]}%\" \"\\n\")\n",
"\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"for key,value in inventory.items():\n",
" print (f\"The actual quantity of item {key} is: {value}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa6bbaa8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "802b1e0f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +151,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down