conditional

Problems on conditionals solutions

January 12, 20247 min read

Problem 1: Loan Eligibility Checker

Description: Write a Python program to determine if an applicant is eligible for a loan based on their salary and credit score. Eligibility requires a salary of at least $50,000 and a credit score of 700 or above.

Sample Input:

salary = 60000
credit_score = 750

Expected Output:

"Loan Eligibility: Approved"

Solution:

salary = 60000
credit_score = 750

if salary >= 50000 and credit_score >= 700:
    print("Loan Eligibility: Approved")
else:
    print("Loan Eligibility: Denied")

Explanation: This program uses an if-else statement to check if both the salary and credit score criteria are met. If both conditions are true, the loan is approved; otherwise, it is denied.


Problem 2: Investment Risk Categorization

Description: Classify an investment as 'High Risk', 'Moderate Risk', or 'Low Risk' based on its return rate. Returns above 10% are 'High Risk', between 5% and 10% are 'Moderate Risk', and below 5% are 'Low Risk'.

Sample Input:

return_rate = 7

Expected Output:

"Investment Risk: Moderate Risk"

Solution:

return_rate = 7

if return_rate > 10:
    print("Investment Risk: High Risk")
elif 5 <= return_rate <= 10:
    print("Investment Risk: Moderate Risk")
else:
    print("Investment Risk: Low Risk")

Explanation: The program uses if-elif-else statements to categorize the investment based on its return rate.


Problem 3: Tax Bracket Calculation

Description: Calculate the tax bracket of a person based on their annual income. Assume the following brackets: 0-50k: 10%, 50k-100k: 20%, Above 100k: 30%.

Sample Input:

income = 75000

Expected Output:

"Tax Bracket: 20%"

Solution:

income = 75000

if income <= 50000:
    print("Tax Bracket: 10%")
elif income <= 100000:
    print("Tax Bracket: 20%")
else:
    print("Tax Bracket: 30%")

Explanation: The program uses if-elif-else statements to determine the tax bracket based on the given annual income.


Problem 4: Adjusted Gross Income Calculator

Description: Calculate the adjusted gross income (AGI) by deducting a standard deduction of $12,400, unless the individual has specified itemized deductions greater than the standard deduction.

Sample Input:

gross_income = 50000
itemized_deductions = 15000

Expected Output:

"Adjusted Gross Income: $35000"

Solution:

gross_income = 50000
itemized_deductions = 15000
standard_deduction = 12400

if itemized_deductions > standard_deduction:
    agi = gross_income - itemized_deductions
else:
    agi = gross_income - standard_deduction

print("Adjusted Gross Income:", agi)

Explanation: The program uses an if-else statement to decide whether to apply the standard deduction or itemized deductions and then calculates the AGI accordingly.


Problem 5: Bonus Eligibility

Description: Determine if an employee is eligible for a bonus. Employees with more than 5 years of service and a performance rating of 8 or above (out of 10) are eligible for a bonus.

Sample Input:

years_of_service = 6
performance_rating = 8.5

Expected Output:

"Bonus Eligibility: Yes"

Solution:

years_of_service = 6
performance_rating = 8.5

if years_of_service > 5 and performance_rating >= 8:
    print("Bonus Eligibility: Yes")
else:
    print("Bonus Eligibility: No")

Explanation: This program checks if both conditions (years of service and performance rating) are met using an if-else statement. If both are true, the employee is eligible for a bonus.


Problem 6: Stock Profit/Loss Calculator with Multiple Conditions

Description: Develop a Python program for stock traders to calculate their profit or loss from stock transactions. The program should ask users to input their purchase price, selling price, and the number of shares they traded. Based on these inputs, the program calculates the total profit or loss. Additionally, it categorizes the profit or loss into 'High', 'Moderate', or 'Low' based on predefined thresholds: profits or losses above $2000 are 'High', between $1000 and $2000 are 'Moderate', and below $1000 are 'Low'. For losses, just label them as 'Loss'.

Sample Input:

Enter purchase price: 100
Enter selling price: 121
Enter number of shares: 50

Expected Output:

Profit: $1050.00
Profit Category: Moderate

Solution:

purchase_price = float(input("Enter purchase price: "))
selling_price = float(input("Enter selling price: "))
shares = int(input("Enter number of shares: "))

profit_loss = (selling_price - purchase_price) * shares
category = ""

if profit_loss > 0:
    print("Profit: $", profit_loss)
    if profit_loss > 2000:
        category = "High"
    elif profit_loss > 1000:
        category = "Moderate"
    else:
        category = "Low"
else:
    print("Loss: $", -profit_loss)
    category = "Loss"

print("Profit/Loss Category:", category)

Explanation: This program uses nested if statements to first determine if the outcome is a profit or a loss and then categorizes the profit based on its amount.


Problem 7: Customized Loan Interest Rate

Description: Create a Python program for a bank that calculates the interest rate on loans. The interest rate is determined by the loan amount and the applicant's credit score. For loan amounts over $50,000: credit scores 750 and above get a 3.0% rate, scores between 700-749 get 3.5%, and below 700 get 4.0%. For loan amounts $50,000 and below, the rates is 2% for the same irrespective of credit score brackets.

Sample Input:

Enter loan amount: 50000
Enter credit score: 720

Expected Output:

Interest Rate: 3.5%

Solution:

loan_amount = float(input("Enter loan amount: "))
credit_score = int(input("Enter credit score: "))

if loan_amount > 50000:
    if credit_score >= 750:
        interest_rate = 3.0
    elif credit_score >= 700:
        interest_rate = 3.5
    else:
        interest_rate = 4.0
else:
        interest_rate = 2


print("Interest Rate:", interest_rate, "%")

Explanation: The program calculates the interest rate based on nested if conditions, varying by loan amount and credit score.


Problem 8: Financial Product Recommendation

Description: Design a Python program for financial advisors to recommend products to clients based on age and investment amount. If the client is under 40 years old and invests $10,000 or more, recommend 'Mutual Funds'. If under 40 and investing less, recommend a 'Savings Account'. For clients 40 and older, if they invest $20,000 or more, recommend 'Government Bonds'; otherwise, recommend a 'Fixed Deposit'.

Sample Input:

Enter your age: 30
Enter your investment amount: 10000

Expected Output:

"Recommended Product: Mutual Funds"

Solution:

age = int(input("Enter your age: "))
investment_amount = float(input("Enter your investment amount: "))

if age < 40:
    if investment_amount >= 10000:
        product = "Mutual Funds"
    else:
        product = "Savings Account"
else:
    if investment_amount >= 20000:
        product = "Government Bonds"
    else:
        product = "Fixed Deposit"

print("Recommended Product:", product)

Explanation: This program uses nested if conditions to recommend a financial product based on the user's age and investment amount.


Problem 9: Credit Card Eligibility Checker

Description: Create a Python script for financial institutions to determine a customer's eligibility for a credit card. The script should ask for the customer's age, annual income, and whether they are employed. To be eligible, customers must be at least 18 years old, earn at least $20,000 annually, and be employed. The program outputs whether the customer is approved or denied for the credit card.

Sample Input:

Enter your age: 25
Enter your annual income: 30000
Are you employed? (yes/no): yes

Expected Output:

"Credit Card Eligibility: Approved"

Solution:

age = int(input("Enter your age: "))
income = float(input("Enter your annual income: "))
employment_status = input("Are you employed? (yes/no): ")

if age >= 18 and income >= 20000 and employment_status == "yes":
    print("Credit Card Eligibility: Approved")
else:
    print("Credit Card Eligibility: Denied")

Problem 10: Mortgage Interest Rate Estimator

Description: Build a Python application for potential homebuyers to estimate their mortgage interest rate. The rate depends on their credit score and the down payment percentage of the house price. For credit scores 750 and above, the base rate is 2.7%; for scores between 700-749, it's 3.0%, and for scores below 700, it's 3.5%. An additional 0.5% is added to the base rate if the down payment is less than 20% of the house price.

Sample Input:

Enter your credit score: 720
Enter your down payment percentage (as a decimal): 0.20

Expected Output:

"Estimated Mortgage Interest Rate: 3%"

Solution:

credit_score = int(input("Enter your credit score: "))
down_payment_percentage = float(input("Enter your down payment percentage (as a decimal): "))

if credit_score >= 750:
    base_rate = 2.7
elif 700 <= credit_score < 750:
    base_rate = 3.0
else:
    base_rate = 3.5

if down_payment_percentage < 0.20:
    base_rate += 0.5

print("Estimated Mortgage Interest Rate:", base_rate, "%")

Explanation: The program calculates the mortgage interest rate based on the user's credit score and down payment percentage using nested if statements.

blog author image

sunil s

Quant Developer & Mentor

Back to Blog