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
76 changes: 76 additions & 0 deletions .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"# Lab | Data Structures "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders\n",
"\n",
"As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"2. Create an empty dictionary called `inventory`.\n",
"\n",
"3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"\n",
"4. Create an empty set called `customer_orders`.\n",
"\n",
"5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n",
"\n",
"6. Print the products in the `customer_orders` set.\n",
"\n",
"7. Calculate the following order statistics:\n",
" - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
" - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
" \n",
" Store these statistics in a tuple called `order_status`.\n",
"\n",
"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",
"\n",
"9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"\n",
"10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
}
],
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
108 changes: 108 additions & 0 deletions .virtual_documents/lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@






# Step 1: Define the list of products
products = ["t-shirt", "mug", "hat", "book", "keychain"]
print(f"✅ Products defined: {products}")


# Step 2: Create an empty inventory dictionary
inventory = {}
print("✅ Empty inventory dictionary created.")


# Step 3: Input inventory quantity for each product
print("\n--- Inventory Setup ---")
for product in products:
while True:
try:
# Ask the user for the quantity of the current product
quantity = int(input(f"Enter the quantity available for '{product}': "))
if quantity >= 0:
inventory[product] = quantity
break
else:
print("Quantity must be a non-negative integer.")
except ValueError:
print("Invalid input. Please enter a whole number.")

print(f"\n📦 Initial Inventory: {inventory}")


# Step 4: Create an empty set for customer orders
customer_orders = set()
print("✅ Empty customer orders set created.")


# Step 5: Input three customer orders
print("\n--- Customer Orders ---")
products_lower = [p.lower() for p in products] # For case-insensitive validation
products_set = set(products)

for i in range(1, 4):
while True:
order_input = input(f"Enter the name of product #{i} (options: {', '.join(products)}): ").lower()
# Check if the entered product is in the list (case-insensitive check)
if order_input in products_lower:
# Add the correctly cased product name (if found) to the set
# Find the original cased name from the list
correct_product_name = next(p for p in products if p.lower() == order_input)
customer_orders.add(correct_product_name)
break
else:
print(f"'{order_input}' is not a valid product. Please choose from the list.")


# Step 6: Print the products in the customer_orders set
print("\n🛒 Products in Customer Orders Set:")
print(customer_orders)


# Step 7: Calculate Order Statistics
# The total number of unique products ordered is the size of the set
total_products_ordered = len(customer_orders)


# The total available unique products is the size of the initial products list
total_available_products = len(products)


# Calculate the percentage
# (Unique products ordered / Total unique products) * 100
percentage_ordered = (total_products_ordered / total_available_products) * 100

# Store statistics in a tuple
order_status = (total_products_ordered, percentage_ordered)

# Print the order statistics
print("\n--- Order Statistics ---")
print("Order Statistics:")
print(f"Total Products Ordered: {order_status[0]}")
print(f"Percentage of Products Ordered: {order_status[1]:.2f}%") # Format to 2 decimal places


# Step 8: Update the inventory
print("\n--- Updating Inventory ---")
# Subtract 1 from the quantity of each product in the customer_orders set
for product_name in customer_orders:
# Check if the product exists in the inventory and its quantity is > 0
if product_name in inventory and inventory[product_name] > 0:
inventory[product_name] -= 1
print(f"⬇️ Subtracted 1 from '{product_name}'.")
elif product_name in inventory and inventory[product_name] == 0:
print(f"⚠️ Warning: '{product_name}' was ordered but inventory is already 0. Inventory remains 0.")
else:
# This case should not happen if input validation is strict, but is good practice
print(f"Product '{product_name}' not found in inventory. Cannot update.")


# Step 9: Print the updated inventory
print("\n📝 Updated Inventory:")
for product, quantity in inventory.items():
print(f"{product}: {quantity}")



Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"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.13.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Lab | Data Structures ","metadata":{"tags":[]}},{"cell_type":"markdown","source":"## Exercise: Managing Customer Orders\n\nAs part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n\nFollow the steps below to complete the exercise:\n\n1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n# Step 1: Define the list of products\nproducts = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\nprint(f\"Products defined: {products}\")\n2. Create an empty dictionary called `inventory`.\n\n3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n\n4. Create an empty set called `customer_orders`.\n\n5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n\n6. Print the products in the `customer_orders` set.\n\n7. Calculate the following order statistics:\n - Total Products Ordered: The total number of products in the `customer_orders` set.\n - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n \n Store these statistics in a tuple called `order_status`.\n\n8. 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\n9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n\n10. Print the updated inventory, displaying the quantity of each product on separate lines.\n\nSolve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. ","metadata":{}},{"cell_type":"code","source":"# Step 1: Define the list of products\nproducts = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\nprint(f\"✅ Products defined: {products}\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"✅ Products defined: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"}],"execution_count":1},{"cell_type":"code","source":"# Step 2: Create an empty inventory dictionary\ninventory = {}\nprint(\"✅ Empty inventory dictionary created.\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"✅ Empty inventory dictionary created.\n"}],"execution_count":2},{"cell_type":"code","source":"# Step 3: Input inventory quantity for each product\nprint(\"\\n--- Inventory Setup ---\")\nfor product in products:\n while True:\n try:\n # Ask the user for the quantity of the current product\n quantity = int(input(f\"Enter the quantity available for '{product}': \"))\n if quantity >= 0:\n inventory[product] = quantity\n break\n else:\n print(\"Quantity must be a non-negative integer.\")\n except ValueError:\n print(\"Invalid input. Please enter a whole number.\")\n\nprint(f\"\\n📦 Initial Inventory: {inventory}\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"\n--- Inventory Setup ---\n"},{"name":"stdin","output_type":"stream","text":"Enter the quantity available for 't-shirt': c\n"},{"name":"stdout","output_type":"stream","text":"Invalid input. Please enter a whole number.\n"},{"name":"stdin","output_type":"stream","text":"Enter the quantity available for 't-shirt': 5\nEnter the quantity available for 'mug': 6\nEnter the quantity available for 'hat': 7\nEnter the quantity available for 'book': 8\nEnter the quantity available for 'keychain': 9\n"},{"name":"stdout","output_type":"stream","text":"\n📦 Initial Inventory: {'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 8, 'keychain': 9}\n"}],"execution_count":3},{"cell_type":"code","source":"# Step 4: Create an empty set for customer orders\ncustomer_orders = set()\nprint(\"✅ Empty customer orders set created.\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"✅ Empty customer orders set created.\n"}],"execution_count":4},{"cell_type":"code","source":"# Step 5: Input three customer orders\nprint(\"\\n--- Customer Orders ---\")\nproducts_lower = [p.lower() for p in products] # For case-insensitive validation\nproducts_set = set(products)\n\nfor i in range(1, 4):\n while True:\n order_input = input(f\"Enter the name of product #{i} (options: {', '.join(products)}): \").lower()\n # Check if the entered product is in the list (case-insensitive check)\n if order_input in products_lower:\n # Add the correctly cased product name (if found) to the set\n # Find the original cased name from the list\n correct_product_name = next(p for p in products if p.lower() == order_input)\n customer_orders.add(correct_product_name)\n break\n else:\n print(f\"'{order_input}' is not a valid product. Please choose from the list.\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"\n--- Customer Orders ---\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): lion\n"},{"name":"stdout","output_type":"stream","text":"'lion' is not a valid product. Please choose from the list.\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): hourse\n"},{"name":"stdout","output_type":"stream","text":"'hourse' is not a valid product. Please choose from the list.\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): panda\n"},{"name":"stdout","output_type":"stream","text":"'panda' is not a valid product. Please choose from the list.\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): tiger\n"},{"name":"stdout","output_type":"stream","text":"'tiger' is not a valid product. Please choose from the list.\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): zebra\n"},{"name":"stdout","output_type":"stream","text":"'zebra' is not a valid product. Please choose from the list.\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): ziraf\n"},{"name":"stdout","output_type":"stream","text":"'ziraf' is not a valid product. Please choose from the list.\n"},{"name":"stdin","output_type":"stream","text":"Enter the name of product #1 (options: t-shirt, mug, hat, book, keychain): t-shirt\nEnter the name of product #2 (options: t-shirt, mug, hat, book, keychain): mug\nEnter the name of product #3 (options: t-shirt, mug, hat, book, keychain): hat\n"}],"execution_count":5},{"cell_type":"code","source":"# Step 6: Print the products in the customer_orders set\nprint(\"\\n🛒 Products in Customer Orders Set:\")\nprint(customer_orders)","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"\n🛒 Products in Customer Orders Set:\n{'t-shirt', 'hat', 'mug'}\n"}],"execution_count":6},{"cell_type":"code","source":"# Step 7: Calculate Order Statistics\n# The total number of unique products ordered is the size of the set\ntotal_products_ordered = len(customer_orders)","metadata":{"trusted":true},"outputs":[],"execution_count":7},{"cell_type":"code","source":"# The total available unique products is the size of the initial products list\ntotal_available_products = len(products)","metadata":{"trusted":true},"outputs":[],"execution_count":8},{"cell_type":"code","source":"# Calculate the percentage\n# (Unique products ordered / Total unique products) * 100\npercentage_ordered = (total_products_ordered / total_available_products) * 100\n\n# Store statistics in a tuple\norder_status = (total_products_ordered, percentage_ordered)\n\n# Print the order statistics\nprint(\"\\n--- Order Statistics ---\")\nprint(\"Order Statistics:\")\nprint(f\"Total Products Ordered: {order_status[0]}\")\nprint(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\") # Format to 2 decimal places","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"\n--- Order Statistics ---\nOrder Statistics:\nTotal Products Ordered: 3\nPercentage of Products Ordered: 60.00%\n"}],"execution_count":9},{"cell_type":"code","source":"# Step 8: Update the inventory\nprint(\"\\n--- Updating Inventory ---\")\n# Subtract 1 from the quantity of each product in the customer_orders set\nfor product_name in customer_orders:\n # Check if the product exists in the inventory and its quantity is > 0\n if product_name in inventory and inventory[product_name] > 0:\n inventory[product_name] -= 1\n print(f\"⬇️ Subtracted 1 from '{product_name}'.\")\n elif product_name in inventory and inventory[product_name] == 0:\n print(f\"⚠️ Warning: '{product_name}' was ordered but inventory is already 0. Inventory remains 0.\")\n else:\n # This case should not happen if input validation is strict, but is good practice\n print(f\"Product '{product_name}' not found in inventory. Cannot update.\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"\n--- Updating Inventory ---\n⬇️ Subtracted 1 from 't-shirt'.\n⬇️ Subtracted 1 from 'hat'.\n⬇️ Subtracted 1 from 'mug'.\n"}],"execution_count":10},{"cell_type":"code","source":"# Step 9: Print the updated inventory\nprint(\"\\n📝 Updated Inventory:\")\nfor product, quantity in inventory.items():\n print(f\"{product}: {quantity}\")","metadata":{"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":"\n📝 Updated Inventory:\nt-shirt: 4\nmug: 5\nhat: 6\nbook: 8\nkeychain: 9\n"}],"execution_count":11},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}
Loading