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
236 changes: 236 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
{
"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": "2b59555c-b038-4877-b244-90ea7376b51d",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "307c168c-979b-458b-8dae-e7ce6795f910",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7d287ae8-a497-4821-a6bd-55cd93c5f9f4",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please add the quantity for t-shirt 5\n",
"Please add the quantity for mug 9\n",
"Please add the quantity for hat 7\n",
"Please add the quantity for book 4\n",
"Please add the quantity for keychain 6\n"
]
}
],
"source": [
"for product in products:\n",
" quantity = int(input(\"Please add the quantity for \" + product))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "205ef1fe-2388-46a5-bf40-a598c9ab9436",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 9, 'hat': 7, 'book': 4, 'keychain': 6}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0c66741a-f4a9-4dea-9055-eebccb5a1ca6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please add the product you would like to order: book\n",
"Would you like to continue ordering? Please answer with yes or no. yes\n",
"Please add the product you would like to order: mug\n",
"Would you like to continue ordering? Please answer with yes or no. yes\n",
"Please add the product you would like to order: hat\n",
"Would you like to continue ordering? Please answer with yes or no. no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your order.\n"
]
}
],
"source": [
"print (\"Available products: \", products)\n",
"customer_orders = set()\n",
"order_again = \"yes\" \n",
"while order_again == \"yes\": \n",
" product = input(\"Please add the product you would like to order:\")\n",
" customer_orders.add(product)\n",
" order_again = input(\"Would you like to continue ordering? Please answer with yes or no.\").lower().strip()\n",
"\n",
"print (\"Thank you for your order.\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2960eba1-2565-40eb-9c2b-2ab307656430",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 9, 'hat': 7, 'book': 4, 'keychain': 6}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5deaa9d9-4851-4ea2-9cb5-301d037acac4",
"metadata": {},
"outputs": [],
"source": [
"for product in customer_orders:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "028c896f-d618-4a5c-b291-eb0abeecdbab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 8, 'hat': 6, 'book': 3, 'keychain': 6}\n"
]
}
],
"source": [
"print (inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fd8d8e5-afb8-4a13-9545-25d16a599850",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "92108459-e3ca-4b11-947a-94bdb77e413d",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading