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
290 changes: 290 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
{
"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": 6,
"id": "737186fb-baab-4573-801b-4afc86142fe6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# defining a list of products\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(\"Available products:\", products)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "09a4d3eb-bf0a-4c5b-a203-e3b29f8c2415",
"metadata": {},
"outputs": [],
"source": [
"# creating an empty iventory\n",
"\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4cf96427-0da5-4c4c-9eab-28c5b9fda659",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter the quantity of each prodcut:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for t-shirt: 67\n",
"Enter quantity for mug: 87\n",
"Enter quantity for hat: 56\n",
"Enter quantity for book: 34\n",
"Enter quantity for keychain: 45\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current Inventory: {'t-shirt': 67, 'mug': 87, 'hat': 56, 'book': 34, 'keychain': 45}\n"
]
}
],
"source": [
"# ask the user for the quantity of each product using a loop\n",
"\n",
"print(\"Please enter the quantity of each prodcut:\")\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"print (\"Current Inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a498fa34-fc80-4603-a46a-8338a0655302",
"metadata": {},
"outputs": [],
"source": [
"# create an empty set for customer orders\n",
"\n",
"customer_orders = set()\n",
"add_another = 'yes'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2515900b-2291-4215-82be-80d3e8e14275",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the product a customer wants to order: hat\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter the product a customer wants to order: book\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter the product a customer wants to order: keychain\n",
"Do you want to add another product? (yes/no): no\n"
]
}
],
"source": [
"# use a while loop and conditional logic to add prodcuts\n",
"\n",
"while add_another.lower() == 'yes':\n",
" order_item = input(\"Enter the product a customer wants to order: \")\n",
" if order_item in products:\n",
" customer_orders.add(order_item)\n",
" add_another = input(\"Do you want to add another product? (yes/no): \")\n",
" else:\n",
" print(\"That product is not available. Please choose from the list again\")\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "97e0ad40-2a12-4968-9470-d099a869c7fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Products in customer order: {'hat', 'keychain', 'book'}\n"
]
}
],
"source": [
"# print the products in the customer_orders set\n",
"\n",
"print(\"Products in customer order:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4e5a5ee8-f339-45c2-ba71-5b85f7978e3b",
"metadata": {},
"outputs": [],
"source": [
"# calculate order statistics\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4b4d4a80-a6ba-4e15-9bb1-2720473e3e67",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"# store statistics in a tuple and print \n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e17beb4d-67b0-438b-be25-54102015e430",
"metadata": {},
"outputs": [],
"source": [
"# update the inventory only for ordered products using a loop\n",
"\n",
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "7f82e8ea-c1b9-4e7a-9982-ab5275ea3993",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 67\n",
"mug: 87\n",
"hat: 55\n",
"book: 33\n",
"keychain: 44\n"
]
}
],
"source": [
"# print the updated inventory\n",
"\n",
"print (\"Updated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8be15f1-12d2-4500-9333-6f8879372e8c",
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading