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
111 changes: 109 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,118 @@
"\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": 28,
"id": "304a7305",
"metadata": {},
"outputs": [],
"source": [
"inventory = {\n",
" 't-shirt': 5,\n",
" 'mug': 6,\n",
" 'hat': 7,\n",
" 'book': 8,\n",
" 'keychain': 5\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "c902073c",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "eefa6c60",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your order has been successfully placed!\n"
]
}
],
"source": [
"# Use a dictionary for customer_orders to track quantities\n",
"customer_orders = {}\n",
"\n",
"while True:\n",
" product = input(\"Enter the name of the product you want to order: \").strip().lower()\n",
" \n",
" if product in inventory:\n",
" try:\n",
" quantity = int(input(f\"How many {product}s would you like to order? \"))\n",
" except ValueError:\n",
" print(\"Please enter a valid number.\")\n",
" continue\n",
"\n",
" if quantity > inventory[product]:\n",
" print(f\"Sorry, we only have {inventory[product]} {product}(s) available.\")\n",
" continue\n",
"\n",
" if product in customer_orders:\n",
" customer_orders[product] += quantity\n",
" else:\n",
" customer_orders[product] = quantity\n",
" else:\n",
" print(\"Sorry, we don't have that product. Please choose from the available products.\")\n",
" continue\n",
"\n",
" another_order = input(\"Would you like to order another product? (yes/no): \").strip().lower()\n",
" if another_order != 'yes':\n",
" break\n",
"\n",
"order_successful = True\n",
"for product, quantity in customer_orders.items():\n",
" if quantity > inventory[product]:\n",
" print(f\"Sorry, we only have {inventory[product]} {product}(s) available. Your order for {quantity} cannot be fulfilled.\")\n",
" order_successful = False\n",
"\n",
"if order_successful and customer_orders:\n",
" for product, quantity in customer_orders.items():\n",
" inventory[product] -= quantity \n",
" print(\"Your order has been successfully placed!\")\n",
"elif not customer_orders:\n",
" print(\"No products were ordered.\")\n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "ca318829",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 3, 'mug': 0, 'hat': 7, 'book': 8, 'keychain': 5}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +162,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down