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
206 changes: 203 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,213 @@
"\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": 1,
"id": "4b36a05d-191d-4933-9171-9392e51184d3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"Enter the quantity for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Quantity for t-shirt: 2\n",
"Quantity for mug: 3\n",
"Quantity for hat: 2\n",
"Quantity for book: 1\n",
"Quantity for keychain: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Final inventory:\n",
"{'t-shirt': 2, 'mug': 3, 'hat': 2, 'book': 1, 'keychain': 2}\n"
]
}
],
"source": [
"## 1 . Look at your code from the lab data structures, and improve repeated code with loops\n",
"\n",
"# Define a list of products\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(\"Available products:\", products)\n",
"\n",
"# Create an empty inventory dictionary\n",
"inventory = {}\n",
"\n",
"# Ask the user to input the quantity of each product\n",
"print(\"\\nEnter the quantity for each product:\")\n",
"\n",
"for item in products:\n",
" while True: \n",
" quantity = input(f\"Quantity for {item}: \")\n",
"\n",
" \n",
" if quantity.isdigit():\n",
" inventory[item] = int(quantity)\n",
" break \n",
" else:\n",
" print(\"Invalid input. Please enter a number.\")\n",
"\n",
"# Print final inventory\n",
"print(\"\\nFinal inventory:\")\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c07c3fad-c992-4134-be3a-d4f8212d07e5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Add products to the customer's order.\n",
"Type the product name exactly as in the product list.\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 'keychain' to the order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? (yes/no): tshirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"No more products will be added.\n",
"\n",
"Customer orders: {'keychain'}\n"
]
}
],
"source": [
"#2. Prompt the user to enter the name of a product that a customer wants to order.\n",
"\n",
"customer_orders = set()\n",
"\n",
"print(\"\\nAdd products to the customer's order.\")\n",
"print(\"Type the product name exactly as in the product list.\\n\")\n",
"\n",
"while True:\n",
" # Ask user for a product name\n",
" product = input(\"Enter a product to order: \").strip().lower()\n",
"\n",
" # Add product to the set\n",
" customer_orders.add(product)\n",
" print(f\"Added '{product}' to the order.\")\n",
"\n",
" # Ask if they want to add another product\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
"\n",
" # Stop if user says no\n",
" if another != \"yes\":\n",
" print(\"\\nNo more products will be added.\")\n",
" break\n",
"\n",
"# Print final customer order\n",
"print(\"\\nCustomer orders:\", customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "14b86f01-f242-44ae-a660-d9001a82b69d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Updating inventory based on customer orders...\n",
"Reduced inventory for keychain. New quantity: 1\n"
]
}
],
"source": [
"#3. Instead of updating the inventory by subtracting 1 from the quantity of each product, \n",
"#only do it for the products that were ordered (those in \"customer_orders\").\n",
"\n",
"print(\"\\nUpdating inventory based on customer orders...\")\n",
"\n",
"for item in customer_orders:\n",
" # Check if the ordered product exists in the inventory\n",
" if item in inventory:\n",
" if inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" print(f\"Reduced inventory for {item}. New quantity: {inventory[item]}\")\n",
" else:\n",
" print(f\"Cannot reduce inventory for {item}. Stock is already 0.\")\n",
" else:\n",
" print(f\"'{item}' is not a valid product and cannot be updated.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cb2a6c5-a277-402a-8c9f-418328ae9097",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4587f20e-eb71-463b-a6dd-10f47d0b37dc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0bf5c94-47e4-490f-9ec1-a007faa73a07",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python (myenv)",
"language": "python",
"name": "python3"
"name": "myenv"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -55,7 +255,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down