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
344 changes: 343 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,348 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6ab65829-5008-4058-8cbd-eabcc1903c7d",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'t-shirt': 1, 'mug': 12, 'hat': 5, 'book': 41, 'keychain': 25}\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "319a24a5-feb6-4a76-b6ee-a0d9a236643f",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"My understanding\n",
"1.a:Define a function named `initialize_inventory` that takes `products` as a parameter. \n",
"1:Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"1.b: To initialize the inventory, use the previous dictionnary inventory is needed\n",
"1.c: Create a user request to ask the reset of the inventory {} whatever the {qty} of the {product} ->input(...)\n",
"1.d: create a loop abble to initialize verifying if the unser_input = 0 and cleant it\n",
"\"\"\"\n",
"\n",
"def initialize_inventory(products): \n",
" inventory = {}\n",
" for product in products:\n",
" qty = input(f\"Do you want to reset the qty of the {product} type 0:\").strip()\n",
" if qty == \"0\":\n",
" inventory[product] = 0\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "8a6b5cdc-ac56-4354-b908-b0b4e211ba79",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, \n",
"implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"My understanding\n",
"2. Define a function named `get_customer_orders` that takes no parameters. \n",
"2.a Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"2.a.a Create a user request to ask the name of item in a loop\n",
"2.a.b In order to return the `customer_orders` creation a dictionnary customer_orders= set().\n",
"\"\"\"\n",
"\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" print(f\"Ready to purchasse?\")\n",
" while True:\n",
" user_input = input(f\"Whrine the product name or 'STOP' to finish.\").strip().lower()\n",
" \n",
" if user_input == \"stop\":\n",
" print(f\"End of the order\")\n",
" break\n",
" if user_input in products:\n",
" customer_orders.add(user_input)\n",
" print(f\"{user_input} added to order.\")\n",
" else:\n",
" print(f\"({user_input} is not available\")\n",
" \n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "99edb6c3-c4c5-47a5-872d-87ae633dd99b",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
"Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"My understanding\n",
"3. Def `update_inventory` `customer_orders` and `inventory as parameters. \n",
"3.1 Up to date the inventoy dict including his key as customer_orders set and his value as `inventory dict\n",
"3.1.a Ask to the user to enter an item name or \"STOP\" to break the loop\n",
"3.1.b Add the item name to the customer_order set() \n",
"3.1.c Ask to the user to enter the item qty ; verify if this input isdigit() and add it to the value = invetory[key] += qty\n",
"3.1.d Print inventory up to date\n",
"\n",
"\"\"\"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" print(f\"Ready to update inventory?\")\n",
" for product_name in customer_orders:\n",
" qty_input = input(f\"Enter QUANTITY of '{product_name}': \").strip()\n",
" if qty_input.isdigit():\n",
" qty = int(qty_input)\n",
" \n",
" customer_orders.add(product_name)\n",
" \n",
" if product_name in inventory:\n",
" inventory[product_name] += qty\n",
" else:\n",
" inventory[product_name] = qty\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a17f5576-ae95-4f00-8b64-42f99baf6c1e",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. \n",
"Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
"The function should return these values.\n",
"My understanding\n",
"4. Def `calculate_order_statistics` with `customer_orders` and `products' as parameters. \n",
"4.1 Calculate the orders_stat : total = lens of the content of set list of item + %unique item\n",
"4.2 Return this values :orders_stat(total , %unique)\n",
"\n",
"\"\"\"\n",
"#products is a list of a product that abble to order\n",
"#customer_orders is a set of items chosen from product list \n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" unique_products_ordered = len(set(customer_orders))\n",
" if len(products) > 0:\n",
" percentage_unique = round((unique_products_ordered / len(products)) * 100,2)\n",
" else:\n",
" percentage_unique = 0\n",
" return {\n",
" 'percentage_unique': percentage_unique, \n",
" 'total_products_ordered': total_products_ordered\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3fcda0b1-443e-487e-8acc-41566a1ec57e",
"metadata": {},
"outputs": [],
"source": [
"#total, percentage_unique = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "16bb2eb8-34fb-4e0f-ba6d-7455a6b12900",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. \n",
"Inside the function, implement the code for printing the order statistics.\n",
"\"\"\"\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\n--- Statistiques ---\")\n",
" print(f\"Percentage of unique products ordered : {order_statistics['percentage_unique']}%\")\n",
" print(f\"Total products ordered : {order_statistics['total_products_ordered']}\")\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "4006f4b2-77e2-4a68-95b4-4b560a5fab1d",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
"Inside the function, implement the code for printing the updated inventory.\n",
"\"\"\"\n",
"def print_updated_inventory(inventory):\n",
" print(f\"Inventory of product : {inventory}\")\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3f6efff-7960-42b3-b28f-7c7712286c3b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 23,
"id": "bb7491e6-9cad-42a1-ae77-69169dea67cd",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to reset the qty of the t-shirt type 0: 0\n",
"Do you want to reset the qty of the mug type 0: 0\n",
"Do you want to reset the qty of the hat type 0: 0\n",
"Do you want to reset the qty of the book type 0: 0\n",
"Do you want to reset the qty of the keychain type 0: 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ready to purchasse?\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Whrine the product name or 'STOP' to finish. mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug added to order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Whrine the product name or 'STOP' to finish. hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat added to order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Whrine the product name or 'STOP' to finish. book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book added to order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Whrine the product name or 'STOP' to finish. stop\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"End of the order\n",
"Ready to update inventory?\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter QUANTITY of 'mug': 2\n",
"Enter QUANTITY of 'hat': 4\n",
"Enter QUANTITY of 'book': 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Statistiques ---\n",
"Percentage of unique products ordered : 60.0%\n",
"Total products ordered : 3\n",
"Inventory of product : {'t-shirt': 0, 'mug': 2, 'hat': 4, 'book': 6, 'keychain': 0}\n"
]
}
],
"source": [
"\"\"\"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"My understanding\n",
"7.1 Ask to the user if ready to order\n",
"7.2 launch initialize_inventory(products, qties) to reset the inventory\n",
"7.3 launch get_customer_orders() to allow the custumer purchase\n",
"7.7 launch update_inventory(customer_orders, inventory) up to date the inventory and the order\n",
"7.8 launch calculate_order_statistics(customer_orders, products) \n",
"7.9 launch print_order_statistics(order_statistics)\n",
"7.10 launch print_updated_inventory(inventory)\n",
"\"\"\"\n",
"\n",
"inventory = {'t-shirt': 1, 'mug': 12, 'hat': 5, 'book': 41, 'keychain': 25} # to ensure the reset is running properly\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"user_input = (\"Ready to purchasse? Yes/No\").strip().lower()\n",
"if user_input == \"no\":\n",
" print(f\"End of the order\")\n",
" \n",
"else:\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9820538-17c9-42fb-b82f-95b8e7465ab5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4882e934-4a8e-4723-b3a0-147d7a217c41",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +403,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down