π Day 9: Conditionals - Implementing Business Logic
Business is full of rules and decisions. "If a customer spends over $100, they get a 10% discount." "If inventory is below 50 units, flag it for reorder." In Python, we implement this decision-making process using conditional statements.
Key Conditional Statements
if: Executes a block of code only if a certain condition isTrue.else: Provides an alternative block of code to execute if theifcondition isFalse.elif("else if"): Lets you check for multiple, sequential conditions. Python executes the first block where the condition isTrueand then skips the rest.
# Classifying a customer based on their total spending
if total_spent > 1000:
customer_tier = "Gold"
elif total_spent > 500:
customer_tier = "Silver"
else:
customer_tier = "Standard"
Environment Setup
Before you begin, ensure you have followed the setup instructions in the main README.md to set up your virtual environment and install the required libraries.
Exploring the Refactored Code
The script for this lesson, conditionals.py, has been refactored to encapsulate each business rule into a separate, testable function.
- Review the Code: Open
Day_09_Conditionals/conditionals.py. Notice the functionscalculate_discount_percent(),calculate_shipping_cost(), andcalculate_employee_bonus(). - Run the Script: From the root directory of the project (
Coding-For-MBA), run the script to see the functions in action:python Day_09_Conditionals/conditionals.py - Run the Tests: You can run the tests for this lesson to verify the correctness of each function under various conditions:
pytest tests/test_day_09.py
π» Exercises: Day 9
-
Discount Policy Automation:
-
In a new script (
my_solutions_09.py), import thecalculate_discount_percentfunction. - Define a
purchase_amountvariable. - Call the function to get the discount rate, then calculate the final price (
purchase_amount * (1 - discount_rate)). -
Print the original amount, the discount rate, and the final price. Test it with amounts like
120,75, and40. -
Shipping Cost Calculator:
-
Import the
calculate_shipping_costfunction. -
Call the function with different combinations of countries (
"USA","Canada","Mexico") and weights (40,60) to see the results. Print a user-friendly message for each case. -
Employee Bonus Calculation:
-
Import the
calculate_employee_bonusfunction. - Test the function by calling it with different scenarios and printing the result:
- A "Sales" employee with a
performance_ratingof 5. - An "Engineering" employee with a
performance_ratingof 4. - Any employee with a rating of 2.
- A "Sales" employee with a
π Fantastic progress! You can now translate complex business rules into code that makes decisions automatically. This is a fundamental skill for automating reports and building analytical models.
Previous: Day 08 β Day 8: Dictionaries - Structuring Complex Business Data β’ Next: Day 10 β Day 10: Loops - Automating Repetitive Business Tasks
You are on lesson 9 of 108.
Additional Materials
- conditionals.ipynb π View on GitHub π Run in Google Colab βοΈ Run in Binder
- solutions.ipynb π View on GitHub π Run in Google Colab βοΈ Run in Binder
conditionals.py
"""
Day 9: Implementing Business Logic with Conditionals (Refactored)
This script demonstrates how to use if, elif, and else statements
to create business rules and make decisions in code. This version is
refactored into functions for better organization and testability.
"""
def calculate_discount_percent(purchase_amount):
"""
Calculates a discount percentage based on the purchase amount.
This demonstrates if/elif/else statements - the foundation of
decision-making in programming. The computer checks each condition
in order and executes the first matching block.
Business Rule:
- Over $100: 10% discount
- Over $50: 5% discount
- Otherwise: No discount
Parameters
----------
purchase_amount : float or int
The total purchase amount in dollars
Returns
-------
float
The discount rate as a decimal (e.g., 0.10 for 10%)
Example
-------
>>> calculate_discount_percent(125.50)
0.10
>>> calculate_discount_percent(75.00)
0.05
"""
# First, validate the input (edge case handling)
if not isinstance(purchase_amount, (int, float)) or purchase_amount < 0:
return 0.0
# Check conditions from most specific to least specific
# The order matters! Python checks these top-to-bottom
if purchase_amount > 100.00:
return 0.10 # 10% discount for big purchases
elif purchase_amount > 50.00:
return 0.05 # 5% discount for medium purchases
else:
return 0.00 # No discount for small purchases
def calculate_shipping_cost(country, order_weight_kg):
"""
Calculates shipping cost based on destination and weight.
This demonstrates NESTED if statements - conditionals inside conditionals.
This is common when you have multiple factors affecting a decision.
Parameters
----------
country : str
Destination country ("USA" or "Canada")
order_weight_kg : float
Weight of the order in kilograms
Returns
-------
int
Shipping cost in dollars, or -1 if shipping not available
Example
-------
>>> calculate_shipping_cost("USA", 60)
75
"""
# First level: Check the country
if country == "USA":
# Second level (nested): Check the weight within USA
if order_weight_kg > 50:
return 75 # Heavy package to USA
else:
return 50 # Light package to USA
elif country == "Canada":
# Second level (nested): Check the weight within Canada
if order_weight_kg > 50:
return 100 # Heavy package to Canada
else:
return 65 # Light package to Canada
else:
# Country not supported
return -1 # Using -1 to indicate "not available"
def calculate_employee_bonus(performance_rating, department, salary):
"""
Calculates an employee's bonus based on performance and department.
This demonstrates complex conditional logic combining multiple factors.
Real business logic often depends on multiple conditions.
Parameters
----------
performance_rating : int
Employee rating (1-5 scale)
department : str
Employee's department
salary : float
Annual salary
Returns
-------
float
Bonus amount in dollars
Example
-------
>>> calculate_employee_bonus(5, "Sales", 80000)
12000.0
"""
# Top performers get bonuses
if performance_rating >= 4:
# Sales department gets higher bonuses (15% vs 10%)
if department == "Sales":
return salary * 0.15 # 15% bonus for high-performing sales
else:
return salary * 0.10 # 10% bonus for high performers in other depts
# Average performers get a smaller bonus
elif performance_rating == 3:
return salary * 0.05 # 5% bonus for meeting expectations
# Below-average performers get no bonus
else:
return 0.0 # No bonus
if __name__ == "__main__":
# --- Example 1: Customer Discount Policy ---
print("--- Customer Discount Calculator ---")
customer_purchase = 125.50
discount_rate = calculate_discount_percent(customer_purchase)
discount = customer_purchase * discount_rate
final = customer_purchase - discount
print(f"Original Price: ${customer_purchase:.2f}")
print(f"Discount ({discount_rate * 100}%): ${discount:.2f}")
print(f"Final Price: ${final:.2f}")
print("-" * 20)
# --- Example 2: Nested Conditionals for Shipping Costs ---
print("--- Shipping Cost Calculator ---")
shipping_country = "Canada"
weight = 60
cost = calculate_shipping_cost(shipping_country, weight)
if cost != -1:
print(f"Shipping to {shipping_country} for a {weight}kg package costs: ${cost}")
else:
print(f"Sorry, shipping to {shipping_country} is not available.")
print("-" * 20)
# --- Example 3: Complex Bonus Calculation ---
print("--- Employee Bonus Calculator ---")
emp_rating = 5
emp_dept = "Sales"
emp_salary = 80000
bonus_amount = calculate_employee_bonus(emp_rating, emp_dept, emp_salary)
print(
f"Employee in {emp_dept} with rating {emp_rating} gets a bonus of: ${bonus_amount:.2f}"
)
print("-" * 20)
solutions.py
"""
Day 9: Solutions to Exercises
"""
# --- Exercise 1: Discount Policy Automation ---
print("--- Solution to Exercise 1 ---")
purchase_amount = 120
if purchase_amount > 100:
discount = 0.10
elif purchase_amount > 50:
discount = 0.05
else:
discount = 0.0
final_price = purchase_amount * (1 - discount)
print(f"Original Amount: ${purchase_amount:.2f}")
print(f"Discount Rate: {discount * 100}%")
print(f"Final Price: ${final_price:.2f}")
print("-" * 20)
# --- Exercise 2: Shipping Cost Calculator ---
print("--- Solution to Exercise 2 ---")
# We can wrap this in a function to easily test different scenarios
def get_shipping_cost(country, order_weight_kg):
cost = 0
if country == "USA":
if order_weight_kg > 50:
cost = 75
else:
cost = 50
elif country == "Canada":
if order_weight_kg > 50:
cost = 100
else:
cost = 65
else:
return "Shipping not available."
return f"Shipping cost: ${cost}"
# Test cases
print(f"USA, 60kg -> {get_shipping_cost('USA', 60)}")
print(f"Canada, 40kg -> {get_shipping_cost('Canada', 40)}")
print(f"Mexico, 30kg -> {get_shipping_cost('Mexico', 30)}")
print("-" * 20)
# --- Exercise 3: Employee Bonus Calculation ---
print("--- Solution to Exercise 3 ---")
def calculate_bonus(rating, department, salary):
bonus_rate = 0
if rating >= 4:
if department == "Sales":
bonus_rate = 0.15
else:
bonus_rate = 0.10
elif rating == 3:
bonus_rate = 0.05
# No need for an else for rating 1 or 2, as bonus_rate is already 0
bonus_amount = salary * bonus_rate
return bonus_amount
# Test cases
salary = 90000
print(
f"Sales employee with rating 5 gets bonus: ${calculate_bonus(5, 'Sales', salary):,.2f}"
)
print(
f"Engineering employee with rating 4 gets bonus: ${calculate_bonus(4, 'Engineering', salary):,.2f}"
)
print(
f"Sales employee with rating 3 gets bonus: ${calculate_bonus(3, 'Sales', salary):,.2f}"
)
print(f"HR employee with rating 2 gets bonus: ${calculate_bonus(2, 'HR', salary):,.2f}")
print("-" * 20)