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
302 changes: 302 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
{
"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": "8da9cdf4-cb3e-424d-b0be-b9a7e252859c",
"metadata": {},
"outputs": [],
"source": [
"#1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "790a6294-0894-4d47-a697-b9a5280e1b53",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1991341-2f51-42fb-a0c8-d72274625df3",
"metadata": {},
"outputs": [],
"source": [
"#2. Create an empty dictionary called inventory."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1ef80dec-8ba3-4503-b67c-7d972b1ee6ca",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18caaaef-f15a-4a3d-ab79-4f60dffa6a52",
"metadata": {},
"outputs": [],
"source": [
"#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."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a5768d3e-e894-4593-bc62-00d9d8a1c67b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt: 50\n",
"Enter the quantity of mug: 40\n",
"Enter the quantity of hat: 20\n",
"Enter the quantity of book: 100\n",
"Enter the quantity of keychain: 50\n"
]
}
],
"source": [
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "050ca8f0-9a6f-4c50-9bb7-6a82cc4fdbc8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Inventory:\n",
"{'t-shirt': 50, 'mug': 40, 'hat': 20, 'book': 100, 'keychain': 50}\n"
]
}
],
"source": [
"print(\"\\nInventory:\")\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cabc7df-cd2f-4ac2-868b-6d8e372e20ec",
"metadata": {},
"outputs": [],
"source": [
"#4. Create an empty set called customer_orders."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "91465da2-9490-4bb0-b347-ba6c07b3c427",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c72e483c-06db-47d9-831d-830a11f1f834",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product to order: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book has been added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? (yes/no): yes\n",
"Enter the name of a product to order: keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"keychain has been added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? (yes/no): yes\n",
"Enter the name of a product to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug has been added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? (yes/no): no\n"
]
}
],
"source": [
"while True:\n",
" # Prompt user for product name\n",
" product_name = input(\"Enter the name of a product to order: \").strip().lower()\n",
" \n",
" # Check if the product is in inventory\n",
" if product_name in inventory:\n",
" customer_orders.add(product_name)\n",
" print(f\"{product_name} has been added to your order.\")\n",
" else:\n",
" print(f\"Sorry, {product_name} is not available in the inventory.\")\n",
" \n",
" # Ask if they want to add another product\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" \n",
" if another != 'yes':\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dee165fd-2973-4f66-b743-54b2e63e10cf",
"metadata": {},
"outputs": [],
"source": [
"# Update inventory for ordered products"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6f75afbc-ffc7-4e22-a20a-18da3bd037d5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: keychain now has 49 remaining.\n",
"Updated inventory: book now has 99 remaining.\n",
"Updated inventory: mug now has 39 remaining.\n",
"Final customer orders: {'keychain', 'book', 'mug'}\n"
]
}
],
"source": [
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"Updated inventory: {product} now has {inventory[product]} remaining.\")\n",
" else:\n",
" print(f\"Sorry, {product} is out of stock.\")\n",
"\n",
"print(\"Final customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4d32398-c3a9-4845-82ca-dc67ccc6692a",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading