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
1 change: 1 addition & 0 deletions lab-python-data-structures
Submodule lab-python-data-structures added at 250c5f
76 changes: 74 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,83 @@
"\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": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# answers:\n",
"\n",
"print(\"\\nInventory\")\n",
"\n",
"# 1. Define a list called product:\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 2. Create an empty inventory dictionary:\n",
"inventory = {}\n",
"\n",
"# 3. Ask the user to enter the quantity available for each product\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the available quantity for '{product}': \"))\n",
" inventory[product] = quantity\n",
" print(f\"{product}: {inventory[product]}\")\n",
"# 4. Create an empty set for customer orders\n",
"customer_orders = set()\n",
"print(customer_orders)\n",
"\n",
"# 5. Ask the user to input three products the customer wants to order:\n",
"\n",
"for i in range(1, 4):\n",
" product_name = input(f\"Enter the name of product #{i}: \").strip().lower() \n",
" #F-strings are string literals prefixed with 'f' or 'F' that contain expressions inside curly braces {}.\n",
" # The user has to type a product from the list (t-shirt, mug, hat, book, keychain)\n",
" if product_name in products:\n",
" customer_orders.add(product_name)\n",
" else:\n",
" print(\"Invalid product, this item will be ignored.\")\n",
"\n",
"# 6. Print the products in the customer_orders set:\n",
"print(\"Products in the customer's order (set):\") \n",
"for item in customer_orders:\n",
" print(\"- \", item)\n",
"\n",
"# 7. Calculate order statistics:\n",
"total_products_ordered = len(customer_orders)\n",
"\n",
"# Percentage of products ordered compared to the total number of products:\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# 8. Print the order statistics:\n",
"print(\"\\nOrder Statistics:\") #order statut : a tuple or list\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\") #order_status[1]: access to the second element\n",
"# : starts the format specification.\n",
"# .2f means:\n",
"# f → format as a floating-point number,\n",
"# .2 → keep 2 digits after the decimal point.\n",
"\n",
"# 9. Update the inventory by subtracting 1 from the quantity of each ordered product:\n",
"for ordered_product in customer_orders:\n",
" if inventory[ordered_product] > 0:\n",
" inventory[ordered_product] -= 1\n",
"\n",
"# 10. Print the updated inventory:\n",
"print(\"\\nUpdated Inventory\")\n",
"for product in products:\n",
" print(f\"{product}: {inventory[product]}\")\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down