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
232 changes: 230 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,239 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# define a list called products \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"# create an empty dictionary called inventory\n",
"# This is where the dictionary is defined and initialized \n",
"inventory = {}\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Step 3: Set Initial Inventory Stock ---\n",
"\n",
"Inventory Dictionary after setup: {'t-shirt': 3, 'mug': 5, 'hat': 7, 'book': 2, 'keychain': 5}\n"
]
}
],
"source": [
"# Assuming these variables are already defined:\n",
"# products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# inventory = {}\n",
"\n",
"# Ask the user to input the quantity for each product in the inventory.\n",
"print(\"\\n--- Step 3: Set Initial Inventory Stock ---\")\n",
"\n",
"for product in products:\n",
" # Ask the user for the quantity for the current product\n",
" quantity_input = input(f\"Enter stock quantity for {product}: \")\n",
" \n",
" # Convert the input string to an integer\n",
" # (Assuming the user enters a valid whole number for simplicity)\n",
" quantity = int(quantity_input)\n",
" \n",
" # Assign the product name as the KEY and the quantity as the VALUE\n",
" inventory[product] = quantity\n",
"\n",
"print(\"\\nInventory Dictionary after setup:\", inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"# create an empty set called customer_orders\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Step 5: Take Customer Order ---\n",
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# Assuming 'products' list and empty 'customer_orders' set are already defined.\n",
"\n",
"# Ask the user for three products and add them to the customer_orders set\n",
"print(\"\\n--- Step 5: Take Customer Order ---\")\n",
"print(\"Available products:\", products)\n",
"\n",
"# Loop three times to ask for three products\n",
"for i in range(3):\n",
" while True:\n",
" # Get input, convert to lowercase, and remove extra spaces\n",
" product_name = input(f\"Enter product #{i+1} name: \").lower().strip()\n",
" \n",
" # Check if the entered product name is valid\n",
" if product_name in products:\n",
" # Add the valid product to the set (sets only store unique items)\n",
" customer_orders.add(product_name)\n",
" break # Exit the inner 'while' loop to move to the next product prompt\n",
" else:\n",
" print(f\"'{product_name}' is not a valid product. Please enter a name from the list.\")\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Products in customer_orders Set: {'hat', 'mug', 't-shirt'}\n"
]
}
],
"source": [
"# Print the products in the customer_orders set\n",
"print(\"Products in customer_orders Set:\", customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"# Calculate the order statistics \n",
"total_products_ordered = len(customer_orders)\n",
"total_available_products = len(products)\n",
"if total_available_products > 0:\n",
" percentage_ordered = (total_products_ordered/ total_available_products) * 100\n",
"else:\n",
" percentage_ordered = 0"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"# Assuming variables were calculate successfully in the previous call:\n",
"total_products_ordered = 3\n",
"total_available_products = 5\n",
"total_percentage_ordered = 60\n",
"\n",
"# Store these statistic in a tuple called order_status\n",
"order_status = (total_products_ordered, total_available_products, total_percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Order Status ---\n",
"Total Products Ordered: <3>\n",
"Total Available Products: <5>\n",
"Percentage of Products Ordered: <60.0%>\n"
]
}
],
"source": [
"# Print the order statistics using the specified format\n",
"print(\"\\n--- Order Status ---\")\n",
"print(f\"Total Products Ordered: <{order_status[0]}>\") # Accesses the first item (Index 0)\n",
"print(f\"Total Available Products: <{order_status[1]}>\") # Accesses the second item (Index 1)\n",
"# Accesses the third item (Index 2) and applies the float format (:.1f)\n",
"print(f\"Percentage of Products Ordered: <{order_status[2]:.1f}%>\")\n"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Step 10: Updating Inventory Stock ---\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 1\n",
"mug: 3\n",
"hat: 5\n",
"book: 2\n",
"keychain: 5\n"
]
}
],
"source": [
"# Update the inventory by subtracting 1 from the quantity of each product ordered.\n",
"# Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"\\n--- Step 10: Updating Inventory Stock ---\")\n",
"\n",
"for product in customer_orders:\n",
" # Check if the product exists in inventory and has stock remaining (to prevent errors)\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1 # Subtract 1 from the stock value\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"# Iterate through the dictionary's items to print the final stock\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +296,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down