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
373 changes: 373 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,373 @@
{
"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": "markdown",
"id": "ddbc1c72-f342-4945-8aed-3d8dd066e749",
"metadata": {},
"source": [
"## Solution"
]
},
{
"cell_type": "markdown",
"id": "40c1241e-aca1-4311-90fd-5ea6a0bcad99",
"metadata": {},
"source": [
"1. Look at your code from the lab data structures, and improve repeated code with loops."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "90454303-01a1-40c2-a416-ed521640c2f8",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please tell me the number of t-shirts: 9\n",
"Please tell me the number of mugs: 9\n",
"Please tell me the number of hats: 9\n",
"Please tell me the number of books: 9\n",
"Please tell me the number of keychains: 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 9, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 9}\n"
]
}
],
"source": [
"#1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#2. Create an empty dictionary called inventory.\n",
"inventory = {}\n",
"\n",
"#3. Ask the user to input the quantity of each product available in the inventory.\n",
" #Use the product names from the products list as keys in the inventory dictionary and assign the respective quantities as values.\n",
"#Once we review loops, the following code can be improved.\n",
"\n",
"for product in products:\n",
" inventory[product]=int(input(f'Please tell me the number of {product}s: '))\n",
"\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "39cfcd2d-cad7-46a9-bd69-55dd9fd45046",
"metadata": {},
"outputs": [],
"source": [
"#4. Create an empty set called customer_orders.\n",
"customer_orders = set()\n",
"total_products_ordered = {}\n"
]
},
{
"cell_type": "markdown",
"id": "62173814-d582-4d1e-bb8a-ec565d0c8c04",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ecd67463-5abe-4041-84ac-8e73222e818b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to order a t-shirt? respond yes or no yes\n",
"How many t-shirts do you want? Currently there are 9 available. 2\n",
"Do you want to order something else? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt'}\n",
"{'t-shirt': 2}\n"
]
}
],
"source": [
"#5. Ask the user to input the name of three products that a customer wants to order\n",
"#(from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\".\n",
"#Add each product name to the customer_orders set.\n",
"\n",
"def askForOrder():\n",
" order_is_complete = False\n",
" \n",
" while order_is_complete == False:\n",
" for product in products:\n",
" wants_product_request = input(f'Do you want to order a {product}? respond yes or no').lower()\n",
" if (wants_product_request == \"yes\"):\n",
" customer_orders.add(product)\n",
" quantity_request = input(f'How many {product}s do you want? Currently there are {inventory[product]} available.')\n",
" total_products_ordered[product] = int(quantity_request)\n",
" # elif wants_product_request == \"no\":\n",
" \n",
" wants_another_product = input(f'Do you want to order something else?').lower()\n",
" if (wants_another_product == \"no\"):\n",
" order_is_complete = True\n",
" break\n",
" \n",
"askForOrder()\n",
"\n",
"\n",
"\n",
"#6. Print the products in the customer_orders set.\n",
"print(customer_orders)\n",
"print(total_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c033fa1-7f92-4e65-b653-87a27f177a5f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 17,
"id": "729773d2-9911-4403-8447-d638469d14c5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_values([2])\n"
]
}
],
"source": [
"#7. Calculate the following order statistics:\n",
"\n",
"#Total Products Ordered: The total number of products in the customer_orders set.\n",
"sum_total_products_ordered = total_products_ordered.values()\n",
"print(sum_total_products_ordered)\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c11321a4-6cef-4299-8af6-903ed4668d8f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0.2222222222222222,)\n"
]
}
],
"source": [
"#Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"\n",
"percentages = []\n",
"\n",
"for product in total_products_ordered:\n",
" percentages.append(total_products_ordered[product]/inventory[product])\n",
"\n",
"#Store these statistics in a tuple called order_status.\n",
"order_status = tuple(percentages)\n",
"print(order_status)\n"
]
},
{
"cell_type": "markdown",
"id": "1db3f242-97d1-4aa4-b4b5-7cfd82638542",
"metadata": {},
"source": [
"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": 26,
"id": "a65869f6-d345-4258-abcf-87cc2892064f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics: \n",
"\n",
"T-shirt\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: : 0.2222222222222222\n",
"\n"
]
},
{
"data": {
"text/plain": [
"\"\\nprint(product_request2.capitalize())\\nprint(f'Total Products Ordered: {total_products_ordered[product_request2]}')\\nprint(f'Percentage of Products Ordered: : {percentage2}\\n')\\n\\nprint(product_request3.capitalize())\\nprint(f'Total Products Ordered: {total_products_ordered[product_request3]}')\\nprint(f'Percentage of Products Ordered: : {percentage3}\\n')\\n\""
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#8. Print the order statistics using the following format:\n",
"#Order Statistics:\n",
"#Total Products Ordered: <total_products_ordered>\n",
"#Percentage of Products Ordered: <percentage_ordered>% \n",
"\n",
"print('Order Statistics: \\n')\n",
"for product in total_products_ordered: \n",
" print(product.capitalize())\n",
" print(f'Total Products Ordered: {total_products_ordered[product]}')\n",
" print(f'Percentage of Products Ordered: : {total_products_ordered[product]/(inventory[product]+total_products_ordered[product])}\\n')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "94e841fe-793d-4add-b7ab-cac2e02e2f4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 7, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 9}\n"
]
}
],
"source": [
"#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"\n",
"for product in total_products_ordered:\n",
" inventory[product]=inventory[product]-total_products_ordered[product] \n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "775cd3f8-82f2-4c7e-bdd4-ea55de6532fc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T-shirts: 7\n",
"\n",
"Mugs: 9\n",
"\n",
"Hats: 9\n",
"\n",
"Books: 9\n",
"\n",
"Keychains: 9\n",
"\n"
]
}
],
"source": [
"#10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"for product in inventory:\n",
" print(f'{product.capitalize()}s: {inventory[product]}\\n')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bd31cac-799a-48b6-86a4-47f5d9093c2e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.14.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading