Skip to content

lab complete #449

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 1 commit 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
76 changes: 76 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"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": null,
"id": "0ece9b4f-055e-48ec-95b5-9ba1248c1dac",
"metadata": {},
"outputs": [],
"source": [
"# 1.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n"
]
}
],
"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
}
94 changes: 91 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,101 @@
"\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": "060e6771-9dbb-403f-b0ed-d0cbbfe2cf6e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for t-shirt: 1\n",
"Enter quantity for mug: 2\n",
"Enter quantity for hat: assd\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a valid integer.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for book: 3\n",
"Enter quantity for keychain: 6\n"
]
}
],
"source": [
"# 1. \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {product: 10 for product in products}\n",
"\n",
"for product in products:\n",
" try:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = max(inventory[product] - quantity, 0)\n",
" except ValueError:\n",
" print(\"Please enter a valid integer.\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a1ecc27a-ce05-4237-8e2c-aa3c2e8e12f1",
"metadata": {},
"outputs": [
{
"ename": "IndentationError",
"evalue": "unindent does not match any outer indentation level (<string>, line 12)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m<string>:12\u001b[0;36m\u001b[0m\n\u001b[0;31m else:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
]
}
],
"source": [
"# 2.\n",
"# a\n",
"product_name=input(\"Enter the product name you want to order: \")\n",
"\n",
"# b\n",
"customer_orders = set()\n",
"\n",
"# c\n",
" if product_name in [product.lower() for product in products]:\n",
" customer_orders.add(product_name)\n",
" print(f\"{product_name} has been added to your order.\")\n",
" else:\n",
" print(\"The product is not available. Please enter a valid product name.\")\n",
"\n",
"# 3\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9633b8c7-16c6-47d0-bad2-3379f7c02df4",
"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 +143,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down