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
197 changes: 194 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,204 @@
"\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 Step: Define products list\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#2 Step: Create an empty dictionary called \"inventory\"\n",
"\n",
"inventory = {}\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available 4\n",
"Enter the quantity of mugs available 6\n",
"Enter the quantity of hats available 3\n",
"Enter the quantity of books available 2\n",
"Enter the quantity of keychains available 8\n"
]
}
],
"source": [
"#3 Step: Get the user input of the quantity of each product available in the inventory\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available\"))\n",
" inventory[product] = quantity\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#4 Step: Create an empty set called \"customer_orders\"\n",
"\n",
"customer_orders = set()\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"#5 Step: Ask the user for three products they want to order\n",
"\n",
"while len(customer_orders) < 3:\n",
" product_order = input(\"Enter the name of a product to order: \")\n",
" if product_order in products:\n",
" customer_orders.add(product_order)\n",
" else:\n",
" print(f\"'{product_order}' is not available. Please choose from the list.\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Products ordered by the customer: {'mug', 'book', 'hat'}\n"
]
}
],
"source": [
"#6 Step: Print the ordered products\n",
"\n",
"print(\"Products ordered by the customer:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"#7 Step: Total product orders in a tuple\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"total_products_available = len(products)\n",
"percentage_ordered = (total_products_ordered / total_products_available) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n"
]
}
],
"source": [
"#8 Step: Print the order statistics in the required format\n",
"\n",
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", order_status[0])\n",
"print(\"Percentage of Products Ordered:\", order_status[1], \"%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The stock of 'mug' has been updated. Remaining quantity: 3\n",
"Unable to fulfill the order for 'book'. Out of stock!\n",
"The stock of 'hat' has been updated. Remaining quantity: 0\n"
]
}
],
"source": [
"#9 Step: Update the inventory y subtracting 1 from the quantity of each product. \n",
"\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" # A message indicating a successful inventory update\n",
" print(f\"The stock of '{product}' has been updated. Remaining quantity: {inventory[product]}\")\n",
" else:\n",
" print(f\"Unable to fulfill the order for '{product}'. Out of stock!\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 4 units available\n",
"mug: 3 units available\n",
"hat: 0 units available\n",
"book: 0 units available\n",
"keychain: 8 units available\n"
]
}
],
"source": [
"#10 Step: Print the updated inventory\n",
"\n",
"print(\"Updated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity} units available\")\n"
]
},
{
"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 +259,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down