Skip to content

solve lab #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
269 changes: 269 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"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": 1,
"id": "278ac3c2",
"metadata": {},
"outputs": [],
"source": [
"# 1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e60fd14c",
"metadata": {},
"outputs": [],
"source": [
"# 2\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "872f0dba",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please provide number of t-shirt available: 8\n",
"Please provide number of mug available: 14\n",
"Please provide number of hat available: 7\n",
"Please provide number of book available: 9\n",
"Please provide number of keychain available: 32\n"
]
}
],
"source": [
"# 3\n",
"for product in products:\n",
" inventory[product] = input(f\"Please provide number of {product} available: \")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d84f8926",
"metadata": {},
"outputs": [],
"source": [
"# 4\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9a80f6b8",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please add product to the order: mug\n",
"Would you like to add another product? (yes/no) yes\n",
"Please add product to the order: hat\n",
"Would you like to add another product? (yes/no) yes\n",
"Please add product to the order: book\n",
"Would you like to add another product? (yes/no) yes\n",
"Please add product to the order: keychain\n",
"Would you like to add another product? (yes/no) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your order!\n"
]
}
],
"source": [
"# 5\n",
"answer = \"yes\"\n",
"while answer != \"no\":\n",
" customer_orders.add(input(f\"Please add product to the order: \"))\n",
" answer = input(\"Would you like to add another product? (yes/no) \")\n",
"if answer == \"no\": # \"else\" is also possible after the while loop\n",
" print(\"Thank you for your order!\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9e644b30",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The products in the order are: {'mug', 'keychain', 'hat', 'book'}\n"
]
}
],
"source": [
"# 6\n",
"print(f\"The products in the order are: {customer_orders}\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "524ff43e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of products in the order is 4.\n"
]
}
],
"source": [
"# 7\n",
"order_length = len(customer_orders)\n",
"print(f\"The number of products in the order is {order_length}.\")\n",
"\n",
"products_length = len(products)\n",
"\n",
"total_products = 0 # more efficient: sum(inventory.values())\n",
"for product in products:\n",
" total_products += int(inventory[product])\n",
"\n",
"perc_ordered = (order_length / total_products) * 100\n",
"\n",
"stats_tuple = (products_length, perc_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3bf729f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 4\n",
"Percentage of Products Ordered: 5.71%\n"
]
}
],
"source": [
"# 8\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_length}\")\n",
"print(f\"Percentage of Products Ordered: {round(perc_ordered, 2)}%\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ab59108e",
"metadata": {},
"outputs": [],
"source": [
"# 9\n",
"for product in customer_orders:\n",
" if product in inventory.keys():\n",
" qty = int(inventory[product])\n",
" qty -= 1\n",
" inventory[product] = str(qty)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2776901b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 8\n",
"mug: 13\n",
"hat: 6\n",
"book: 8\n",
"keychain: 31\n"
]
}
],
"source": [
"# 10\n",
"for product in products:\n",
" print(f\"{product}: {inventory[product]}\")"
]
}
],
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading