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
317 changes: 312 additions & 5 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"1. Define a function named `initialize_inventory` that takes `productss` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the products names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `productss` as parameters. Inside the function, implement the code for calculating the order statistics (total productss ordered, and percentage of unique productss ordered). The function should return these values.\n",
"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"\n",
Expand All @@ -43,11 +43,318 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b9b0cfd4",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6df4a541",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {} # 1. Initialize empty dictionary\n",
"\n",
" # 2. Start loop for user input\n",
" while True:\n",
" products = input(\"Enter products name (or 'done' to finish): \").lower().strip()\n",
"\n",
" # Exit condition\n",
" if products.lower() == \"done\":\n",
" break\n",
"\n",
" quantity = int(input(f\"Enter quantity for {products}: \"))\n",
"\n",
" # 3. Add to dictionary\n",
" inventory[products] = quantity\n",
"\n",
" # 4. Return completed dictionary\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d4fcb8a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'beer': 50, 'water': 50, 'wine': 50}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "bcac7dad",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(products):\n",
" \n",
" customer_orders = set() \n",
" \n",
" while True:\n",
" products = input(\"Enter products name (or 'done' to finish): \").strip()\n",
"\n",
" # Exit condition\n",
" if products.lower() == \"done\":\n",
" break\n",
"\n",
" # 3. Add to set\n",
" customer_orders.add(products)\n",
"\n",
" # 4. Return completed set\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "50875be5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'beer', 'water', 'wine'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders(products)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "bf1abed4",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n",
"customer_orders = {'t-shirt', 'hat'}"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "f088941c",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" return inventory \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "fadb0a19",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 9, 'mug': 10, 'hat': 9, 'book': 10, 'keychain': 10}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "aa6e8775",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistic (customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" \n",
" # 2. Percentage of unique products ordered\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" \n",
" # 3. Return both values (packed as a tuple)\n",
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "52d9fb7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Ordered: 2\n",
"Percentage Ordered: 40.00%\n"
]
}
],
"source": [
"total_ordered, percentage = calculate_order_statistic(customer_orders, products)\n",
"\n",
"print(f\"Total Ordered: {total_ordered}\")\n",
"print(f\"Percentage Ordered: {percentage:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "ddc07fc2",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistic):\n",
" # Unpack the tuple passed into the parameter\n",
" total_ordered, percentage = order_statistic\n",
" \n",
" # Print the formatted statistics\n",
" print(f\"Total Products Ordered: {total_ordered}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage:.2f}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "9d6256c6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.00%\n"
]
}
],
"source": [
"# 1. Calculate the stats and store the resulting tuple\n",
"stats = calculate_order_statistic(customer_orders, products)\n",
"\n",
"# 2. Pass that tuple into your print function to trigger the print statements\n",
"print_order_statistics(stats)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "09d0622a",
"metadata": {},
"outputs": [],
"source": [
"def print_update_inventory(inventory):\n",
" print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "665cd0bb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 9, 'mug': 10, 'hat': 9, 'book': 10, 'keychain': 10}\n"
]
}
],
"source": [
"print_update_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "ae2051f0",
"metadata": {},
"outputs": [],
"source": [
"def run_store_management(products):\n",
" # 1. Inizializza l'inventario\n",
" inventory = initialize_inventory(products)\n",
" \n",
" # 2. Raccogli gli ordini del cliente\n",
" customer_orders = get_customer_orders(products)\n",
" \n",
" # 3. Aggiorna l'inventario\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" \n",
" # 4. Calcola le statistiche degli ordini\n",
" order_statistics = calculate_order_statistic(customer_orders, products)\n",
" \n",
" # 5. Stampa le statistiche\n",
" print_order_statistics(order_statistics)\n",
" \n",
" # 6. Stampa l'inventario aggiornato\n",
" print_update_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "7a9f2bad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 3\n",
"Percentage of Unique Products Ordered: 60.00%\n",
"{'milk': 59, 'beer': 49, 'water': 29}\n"
]
}
],
"source": [
"run_store_management(products)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv (3.14.6.final.0)",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +368,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down