Ex 10: Loan Eligibility Checker
Problem
We’re building a basic loan eligibility checker for a bank. A customer must meet both an age and an income requirement to be eligible.
Rules
- Define
check_loan_eligibility(age: int, income: float) -> str. - If age is 21 or above and income is 25,000 or above, return "Eligible for loan".
- If age is 21 or above and income is below 25,000, return "Not eligible: Income too low".
- If age is less than 21, return "Not eligible: Age must be 21 or above".
Boilerplate
Copy below code and paste to our IDE for head start.
# Write our code inside this function
def check_loan_eligibility(age: int, income: float) -> str:
pass # remove this
# Call with test data
print(check_loan_eligibility(25, 30000))
print(check_loan_eligibility(22, 15000))
print(check_loan_eligibility(18, 20000))
Expected Output
Eligible for loan
Not eligible: Income too low
Not eligible: Age must be 21 or above
Solution
Only view the solution after trying our best.
Show solution
def check_loan_eligibility(age: int, income: float) -> str:
if age >= 21:
if income >= 25000:
return "Eligible for loan"
return "Not eligible: Income too low"
return "Not eligible: Age must be 21 or above"
# Test calls
print(check_loan_eligibility(25, 30000))
print(check_loan_eligibility(22, 15000))
print(check_loan_eligibility(18, 20000))
Details
Using nested if statements here is cleaner than a long if/elif chain with multiple and conditions. It allows us to separate the age check from the income check, making the logic easier to follow and maintain.