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
178 changes: 178 additions & 0 deletions lab_flow_control_end.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": []
},
"source": [
"# Lab | Flow Control"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
"\n",
"In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n",
"\n",
"You did so without using flow control. Let's go a step further and improve this code.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
"\n",
"2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n",
" \n",
" a. Prompt the user to enter the name of a product that a customer wants to order.\n",
" \n",
" b. Add the product name to the \"customer_orders\" set.\n",
" \n",
" c. Ask the user if they want to add another product (yes/no).\n",
" \n",
" d. Continue the loop until the user does not want to add another product.\n",
"\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": 5,
"id": "ec6628d2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 2, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 6}\n"
]
}
],
"source": [
"#1\n",
"\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" while True:\n",
" product_qt = int(input(f'Enter product quantity for {product}:'))\n",
" if product_qt >= 0:\n",
" inventory[product] = product_qt\n",
" break\n",
" else:\n",
" print('quantity must be positive number')\n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e5bf4ab3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat added to customer orders\n",
"mug added to customer orders\n",
"book added to customer orders\n",
"{'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"#2\n",
"\n",
"customer_orders = set()\n",
"\n",
"\n",
"while True:\n",
" order_item = input('Enter product name to order: ').strip()\n",
" if order_item in products:\n",
" customer_orders.add(order_item)\n",
" print(f'{order_item} added to customer orders')\n",
" while True:\n",
" add_order = input('Order another product? (yes/no):').strip()\n",
" if add_order in ['yes', 'no']:\n",
" break\n",
" else:\n",
" print('Invalid input. Enter \"yes\" or \"no\".')\n",
" if add_order == 'no':\n",
" break\n",
" \n",
"\n",
"print(customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "97014595",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Decremented mug. New quantity: 2\n",
"Decremented hat. New quantity: 3\n",
"Decremented book. New quantity: 4\n",
"{'t-shirt': 2, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 6}\n"
]
}
],
"source": [
"#3\n",
"\n",
"for order_item in customer_orders:\n",
" if order_item in inventory:\n",
" if inventory[order_item] > 0:\n",
" inventory[order_item] -= 1\n",
" print(f\"{order_item}. New quantity: {inventory[order_item]}\")\n",
" else:\n",
" print(f\"'{order_item}' is out of stock.\")\n",
" else:\n",
" print(f\"'{order_item}' not found in inventory.\")\n",
"\n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c16c9a5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}