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
345 changes: 342 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,352 @@
"\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": 1,
"metadata": {},
"outputs": [],
"source": [
"# 1. \n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 2. \n",
"\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity available for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Quantity of t-shirt: 5\n",
"Quantity of mug: 4\n",
"Quantity of hat: 7\n",
"Quantity of book: 8\n",
"Quantity of keychain: 9\n"
]
}
],
"source": [
"# 3. \n",
"\n",
"print(\"Enter the quantity available for each product:\")\n",
"for product in products:\n",
"\n",
" quantity = input(f\"Quantity of {product}: \")\n",
"\n",
" # Repeat until the user enters a valid number\n",
" while not quantity.isdigit():\n",
" print(\"Please enter a valid number.\")\n",
" quantity = input(f\"Quantity of {product}: \")\n",
"\n",
" inventory[product] = int(quantity)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# 4.\n",
"\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product: hat\n",
"Enter the name of a product: book\n",
"Enter the name of a product: mug\n"
]
}
],
"source": [
"# 5.\n",
"\n",
"print(\"\\nEnter three products that the customer wants to order:\")\n",
"for i in range(3):\n",
" order = input(f\"Enter product {i+1}: \").lower()\n",
"\n",
" # Validate input: must exist in products list\n",
" while order not in products:\n",
" print(\"Invalid product. Please enter a valid product from the list.\")\n",
" order = input(f\"Enter product {i+1}: \").lower()\n",
"\n",
" customer_orders.add(order)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Customer has ordered these products:\n",
"- hat\n",
"- book\n",
"- mug\n"
]
}
],
"source": [
"# 6.\n",
"\n",
"print(\"\\nCustomer has ordered these products:\")\n",
"for item in customer_orders:\n",
" print(\"-\", item)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# 7.\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100 "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 8.\n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 9.\n",
"\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 3\n",
"hat: 6\n",
"book: 7\n",
"keychain: 9\n"
]
}
],
"source": [
"# 10.\n",
"\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity available for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Quantity of t-shirt: 3\n",
"Quantity of mug: 5\n",
"Quantity of hat: 7\n",
"Quantity of book: 8\n",
"Quantity of keychain: 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Enter three products that the customer wants to order:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product 1: haty\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid product. Please enter a valid product from the list.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product 1: hat\n",
"Enter product 2: book\n",
"Enter product 3: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Customer has ordered these products:\n",
"- hat\n",
"- book\n",
"- mug\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 3\n",
"mug: 4\n",
"hat: 6\n",
"book: 7\n",
"keychain: 9\n"
]
}
],
"source": [
"# 1. Define a list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 2. Create an empty dictionary for inventory\n",
"inventory = {}\n",
"\n",
"# 3. Ask the user for quantity of each product\n",
"print(\"Enter the quantity available for each product:\")\n",
"for product in products:\n",
"\n",
" quantity = input(f\"Quantity of {product}: \")\n",
"\n",
" # Repeat until the user enters a valid number\n",
" while not quantity.isdigit():\n",
" print(\"Please enter a valid number.\")\n",
" quantity = input(f\"Quantity of {product}: \")\n",
"\n",
" inventory[product] = int(quantity)\n",
"# 4. Create an empty set for customer orders\n",
"customer_orders = set()\n",
"\n",
"# 5. Ask the user for 3 product orders\n",
"print(\"\\nEnter three products that the customer wants to order:\")\n",
"for i in range(3):\n",
" order = input(f\"Enter product {i+1}: \").lower()\n",
"\n",
" # Validate input: must exist in products list\n",
" while order not in products:\n",
" print(\"Invalid product. Please enter a valid product from the list.\")\n",
" order = input(f\"Enter product {i+1}: \").lower()\n",
"\n",
" customer_orders.add(order)\n",
"\n",
"# 6. Print the products ordered\n",
"print(\"\\nCustomer has ordered these products:\")\n",
"for item in customer_orders:\n",
" print(\"-\", item)\n",
"\n",
"# 7. Calculate statistics\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"# 8. Store statistics in a tuple\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# 9. Print order statistics\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"# 10. Update inventory: subtract 1 from ordered products and print\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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 @@ -68,7 +407,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down