Skip to content
Open
Show file tree
Hide file tree
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
262 changes: 262 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"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": 11,
"id": "1abf2b09-6690-45bd-a7af-861b7c37861e",
"metadata": {},
"outputs": [],
"source": [
"products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a387e2f0-b788-47e0-b765-d256b9bcb50f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for tshirt: 22\n",
"Enter quantity for mug: 15\n",
"Enter quantity for hat: 11\n",
"Enter quantity for book: 7\n",
"Enter quantity for keychain: 3\n"
]
}
],
"source": [
"for product in products:\n",
" quantity = input(f\"Enter quantity for {product}: \")\n",
" inventory[product] = int(quantity)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "bdea4509-61ab-4ef3-bfba-3cb57ff820b6",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b7738418-1d35-40bc-abd8-fd58ffea2a99",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product to order: hat\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter product to order: book\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter product to order: keychain\n",
"Do you want to add another product? (yes/no): no\n"
]
}
],
"source": [
"another = \"yes\"\n",
"while another == \"yes\":\n",
" order = input(\"Enter product to order: \")\n",
" customer_orders.add(order)\n",
" another = input(\"Do you want to add another product? (yes/no): \").lower()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "061bc2f5-e924-49c1-94bb-e8e7404c3e2a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'hat', 'keychain'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "4cca7c79-7608-486c-8a11-80787cb54774",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"order_status"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d327f508-9749-44ea-b792-3b02fbc9e31b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_ordered}%\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "eab5aa66-9251-4c3e-aa9b-8670ecb14cdb",
"metadata": {},
"outputs": [],
"source": [
"for product in customer_orders:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "53c76ab2-ca9e-41be-a583-a1245fed1a61",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'tshirt': 22, 'mug': 15, 'hat': 10, 'book': 6, 'keychain': 2}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "79a7d331-63f8-4338-ae9f-29559175d3e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tshirt: 22\n",
"mug: 15\n",
"hat: 10\n",
"book: 6\n",
"keychain: 2\n"
]
}
],
"source": [
"for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4516bdb6-af15-4145-bdf7-f6e91945896b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading