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
258 changes: 256 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,265 @@
"\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": "481c001c",
"metadata": {},
"outputs": [],
"source": [
"# 1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
"\n",
"# 1.1. Define a LIST called `products` that contains the following items:\n",
"# \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c3358ec6",
"metadata": {},
"outputs": [],
"source": [
"# 1.2. Create an empty DICTIONARY called `inventory`\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "432cf69c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'keychain': 8, 't-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7}\n"
]
}
],
"source": [
"# 1.3. Ask the user to input the quantity of each product available in the inventory.\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity of each {product} in the inventory: \" ))\n",
"# Use the product names from the `products` list as keys in the `inventory` dictionary\n",
"# and assign the respective quantities as values.\n",
" inventory[product] = quantity\n",
"\n",
"# Print the `inventory` dictionary to see the available products and their quantities.\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e24231a",
"metadata": {},
"outputs": [],
"source": [
"# 1.4. Create an empty SET called `customer_orders`\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e907fc01",
"metadata": {},
"outputs": [],
"source": [
"# 1.5. Ask the user to input the name of three products that a customer wants to order\n",
"# (from those in the products list, \n",
"# meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\".\n",
"# Add each product name to the `customer_orders` set.\n",
"\n",
"for x in range(3):\n",
" input_product = input(\"Enter three products from those in the products list: \")\n",
" if input_product in products:\n",
" customer_orders.add(input_product)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a693854",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Products in the customer orders: {'hat', 'book', 'mug'}\n"
]
}
],
"source": [
"# 1.6. Print the products in the `customer_orders` set.\n",
"print (\"Products in the customer orders: \", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c7ea02d2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: {60.0} %\n"
]
}
],
"source": [
"# 1.7. Calculate the following order statistics:\n",
"# - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"tot_products_ordered = len(customer_orders)\n",
"print(f\"Total Products Ordered: {tot_products_ordered}\")\n",
" \n",
"# - Percentage of Products Ordered:\n",
"# The percentage of products ordered compared to the total available products.\n",
"percentage_products_ordered = (tot_products_ordered / len(products)) * 100\n",
"print(\"Percentage of Products Ordered:\", {percentage_products_ordered}, \"%\")\n",
"\n",
"# Store these statistics in a tuple called `order_status`\n",
"order_status = (tot_products_ordered, percentage_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "f8c7d8ab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0% \n",
" \n"
]
}
],
"source": [
"# 1.8. Print the order statistics using the following format:\n",
"# ```\n",
"# Order Statistics:\n",
"# Total Products Ordered: <total_products_ordered>\n",
"# Percentage of Products Ordered: <percentage_ordered>% \n",
"# ```\n",
"print(f\"\"\"Order Statistics:\n",
"Total Products Ordered: {tot_products_ordered}\n",
"Percentage of Products Ordered: {percentage_products_ordered}% \n",
" \"\"\")\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3a85f640",
"metadata": {},
"outputs": [],
"source": [
"# 1.9. Update the inventory by subtracting 1 from the quantity of each product.\n",
"# Modify the `inventory` dictionary accordingly.\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "a7bcb644",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory:\n",
"keychain: 8\n",
"t-shirt: 4\n",
"mug: 4\n",
"hat: 5\n",
"book: 6\n"
]
}
],
"source": [
"# 1.10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"Updated inventory:\")\n",
"for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\") \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0856e774",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug added to customer orders\n",
"book added to customer orders\n",
"\n",
"Customer orders: {'hat', 'book', 'mug'}\n",
"Updated inventory: {'keychain': 8, 't-shirt': 4, 'mug': 2, 'hat': 3, 'book': 4}\n"
]
}
],
"source": [
"\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",
"# b. Add the product name to the \"customer_orders\" set.\n",
"# c. Ask the user if they want to add another product (yes/no).\n",
"# d. Continue the loop until the user does not want to add another product.\n",
"\n",
"while True:\n",
" input_product = input(f\"Enter the name of one product from those in the product list: \" ).lower()\n",
" if input_product in products:\n",
" customer_orders.add(input_product)\n",
" print(f\"{input_product} added to customer orders\")\n",
" else:\n",
" print(f\"{input_product} is not in the product list\")\n",
" input_add_another_product = input(\"Do you want to add another product (yes/no)?: \" ).lower()\n",
"\n",
" if input_add_another_product != \"yes\":\n",
" break\n",
"\n",
"# 3. Instead of updating the inventory by subtracting 1 from the quantity of each product,\n",
"# only do it for the products that were ordered (those in \"customer_orders\").\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} is out of stock\")\n",
"\n",
"print(\"Customer orders:\", customer_orders)\n",
"print(\"Updated inventory:\", inventory)\n",
"\n",
"\n",
"\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +309,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down