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
94 changes: 64 additions & 30 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
@@ -1,49 +1,83 @@
{
"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",
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {},
"outputs": [],
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory={}\n",
"\n",
"for product in products:\n",
" quantity=int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product]=quantity\n",
"\n",
"print(inventory)\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",
"# 2\n",
"# improvement of code Lab1\n",
"\n",
"You did so without using flow control. Let's go a step further and improve this code.\n",
"customer_orders=set()\n",
"while True:\n",
" item = input(f\"Enter product you want to order ['t-shirt', 'mug', 'hat', 'book', 'keychain']: \")\n",
" while item not in products:\n",
" item = input(\"❌Invalid product.Please choose from this list ['t-shirt', 'mug', 'hat', 'book', 'keychain']:\")\n",
"\n",
"Follow the steps below to complete the exercise:\n",
" customer_orders.add(item)\n",
"\n",
"1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
" another = input(\"Do you want to order another product? (yes/no): \")\n",
" if another != \"yes\":\n",
" break\n",
" \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",
"#6\n",
"print(\"Products ordered by customer:\")\n",
"for item in customer_orders:\n",
" print(item)\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\")."
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = total_products_ordered / len(products) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"\"\"\"\n",
"Order Statistics:\n",
"Total Products Ordered: <total_products_ordered>\n",
"Percentage of Products Ordered: <percentage_ordered>%\n",
"\"\"\"\n",
"\n",
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", total_products_ordered)\n",
"print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n",
"\n",
"# 3\n",
"# improvement of code Lab1\n",
"\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"Inventory after order:\")\n",
"for item in inventory:\n",
" print(item, inventory[item])\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87f9b525-2668-4d85-8432-7334b192f114",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -55,7 +89,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down