Skip to content
Open
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
170 changes: 167 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,177 @@
"\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": "885f111e-6e87-47d6-9b40-f9f53ba4c9e9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt available in the inventory: 20\n",
"Enter the quantity of mug available in the inventory: 30\n",
"Enter the quantity of hat available in the inventory: 10\n",
"Enter the quantity of book available in the inventory: 80\n",
"Enter the quantity of keychain available in the inventory: 60\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Initial invetory:\n",
"{'t-shirt': 20, 'mug': 30, 'hat': 10, 'book': 80, 'keychain': 60}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of product 1 to order: hat\n",
"Enter the name of product 2 to order: mug\n",
"Enter the name of product 3 to order: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'book', 'hat'}\n"
]
}
],
"source": [
"#1. Look at your code from the lab data structures, and improve repeated code with loops\n",
"\n",
"# Define a list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Create an empty dictionary\n",
"inventory = {}\n",
"\n",
"# The code is already improved with loop for\n",
"\n",
"# We used the loop for to ask the users to input the quantity of each product available in the inventory\n",
"for p in products:\n",
" product_quantity = int(input(f\"Enter the quantity of {p} available in the inventory: \"))\n",
" inventory [p] = product_quantity\n",
" \n",
"print(f\"\\nInitial invetory:\")\n",
"print(inventory)\n",
"\n",
"# Create an empty set called customer_orders to avoid duplicated orders\n",
"customer_orders = set()\n",
"\n",
"# We used the loop for to ask the user to input the name of three products \n",
"for i in range(3):\n",
" product_name = input(f\"Enter the name of product {i+1} to order: \")\n",
" customer_orders.add (product_name)\n",
"\n",
"# Display the customer orders\n",
"print(customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a96779f2-aab8-42f9-8ce9-32b37d636f38",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product to order: hat\n",
"Do you want to add another product ? (yes or no) : yes\n",
"Enter the name of a product to order: mug\n",
"Do you want to add another product ? (yes or no) : yes\n",
"Enter the name of a product to order: book\n",
"Do you want to add another product ? (yes or no) : yes\n",
"Enter the name of a product to order: mug\n",
"Do you want to add another product ? (yes or no) : no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: \n",
"{'mug', 'book', 'hat'}\n"
]
}
],
"source": [
"#2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n",
"customer_orders = set() \n",
"# we create the variable for the loop\n",
"add_product = \"yes\"\n",
"\n",
"#a. Prompt the user to enter the name of a product that a customer wants to order.\n",
"while add_product.lower() == \"yes\":\n",
" product_name = input(f\"Enter the name of a product to order: \").strip().lower()\n",
" \n",
"#b. Add the product name to the \"customer_orders\" set.\n",
" customer_orders.add(product_name)\n",
"\n",
"# c. Ask the user if they want to add another product (yes/no).\n",
" add_product = input(f\"Do you want to add another product ? (yes or no) :\")\n",
"\n",
"\n",
" \n",
" \n",
"#d. Continue the loop until the user does not want to add another product\n",
"\n",
"# continue asking for products until the user says \"no\"\n",
"while add_product.lower() == \"yes\":\n",
" \n",
" # Prompt the user to enter the name of a product\n",
" product_name = input(\"Enter the name of the product: \")\n",
" \n",
" #Add the product name to the set\n",
" customer_orders.add(product_name)\n",
" \n",
" #Ask if the user wants to add another product\n",
" add_product = input(f\"Do you want to add another product ? (yes/no): \") \n",
"\n",
"print (\"Customer orders: \")\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a090fe32-d146-4204-97a7-426b14599ad5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"New quantity: {'t-shirt': 20, 'mug': 29, 'hat': 9, 'book': 79, 'keychain': 60}\n"
]
}
],
"source": [
"# 3. Instead of updating the inventory by subtracting 1 from the quantity of each product, \n",
"# Check whether the ordered product exists in inventory\n",
"for p in customer_orders:\n",
" if p in inventory:\n",
" inventory[p] -=1\n",
"\n",
"print(f\"New quantity: {inventory}\")"
]
}
],
"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 +219,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down