0% found this document useful (0 votes)
3 views9 pages

Code 111

The document outlines a Python program designed to facilitate loan financing for multiple borrowers, allowing user input for personal and loan details. It includes functions for collecting user data, calculating simple and compound interest, and summarizing loan information for each borrower. The program prompts for the number of borrowers, annual interest rate, and type of interest before calculating and displaying the loan details for each borrower.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Code 111

The document outlines a Python program designed to facilitate loan financing for multiple borrowers, allowing user input for personal and loan details. It includes functions for collecting user data, calculating simple and compound interest, and summarizing loan information for each borrower. The program prompts for the number of borrowers, annual interest rate, and type of interest before calculating and displaying the loan details for each borrower.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

# Loan Financing Program for Multiple Borrowers with user-input

interest rate

def collect_user_data():
print("\nEnter personal information:")
name = input("Full Name: ")
national_id = input("National ID: ")
sector = input("Employment Sector: ")
monthly_salary = float(input("Monthly Salary: "))

return {
"name": name,
"national_id": national_id,
"sector": sector,
"monthly_salary": monthly_salary
}

def get_loan_details():
print("\nLoan Details:")
loan_amount = float(input("Desired Loan Amount: "))

print("Choose Loan Duration (months): 12, 24, 36, 48, 60")


while True:
duration = int(input("Loan Duration (months): "))
if duration in [12, 24, 36, 48, 60]:
break
else:
print("Invalid duration. Please choose 12, 24, 36, 48, or 60
months.")

return loan_amount, duration

def calculate_simple_interest(loan_amount, duration,


annual_interest_rate):
total_interest = loan_amount * (annual_interest_rate / 100) *
(duration / 12)
total_amount = loan_amount + total_interest
monthly_payment = total_amount / duration

return monthly_payment, total_interest

def calculate_compound_interest(loan_amount, duration,


annual_interest_rate):
monthly_rate = annual_interest_rate / 12 / 100
total_amount = loan_amount * ((1 + monthly_rate)^duration)
total_interest = total_amount - loan_amount
monthly_payment = total_amount / duration
return monthly_payment, total_interest

def main():
borrowers = []

num_borrowers = int(input("Enter the number of borrowers: "))


annual_interest_rate = float(input("Enter the annual interest rate
(%): "))

print("Choose interest type:")


print("1. Simple Interest")
print("2. Compound Interest")
while True:
interest_choice = input("Enter 1 or 2: ")
if interest_choice in ['1', '2']:
break
else:
print("Invalid choice. Please enter 1 or 2.")

for i in range(num_borrowers):
print(f"\n--- Borrower {i + 1} ---")
user_data = collect_user_data()
loan_amount, duration = get_loan_details()

if interest_choice == '1':
monthly_payment, total_interest =
calculate_simple_interest(loan_amount, duration, annual_interest_rate)
interest_type = "Simple"
else:
monthly_payment, total_interest =
calculate_compound_interest(loan_amount, duration,
annual_interest_rate)
interest_type = "Compound"

borrower_info = {
"user_data": user_data,
"loan_amount": loan_amount,
"duration": duration,
"monthly_payment": monthly_payment,
"total_interest": total_interest,
"annual_interest_rate": annual_interest_rate,
"interest_type": interest_type
}

borrowers.append(borrower_info)

print("\n*** Loan Summaries for All Borrowers ***")


for idx, borrower in enumerate(borrowers, 1):
print(f"\n--- Borrower {idx} ---")
print(f"Name: {borrower['user_data']['name']}")
print(f"National ID: {borrower['user_data']['national_id']}")
print(f"Sector: {borrower['user_data']['sector']}")
print(f"Monthly Salary: {borrower['user_data']
['monthly_salary']:.2f}")
print(f"Loan Amount: {borrower['loan_amount']:.2f}")
print(f"Loan Duration: {borrower['duration']} months")
print(f"Annual Interest Rate: {borrower['annual_interest_rate']}
%")
print(f"Interest Type: {borrower['interest_type']}")
print(f"Total Interest: {borrower['total_interest']:.2f}")
print(f"Monthly Installment (including interest):
{borrower['monthly_payment']:.2f}")

if __name__ == "__main__":
main()
Explanation of Python program :

 Function: collect_user_data():

def collect_user_data():

// Defines a function to collect user (borrower) personal


information.

 print("\nEnter personal information:")

// This to displays a prompt to inform the user to enter their


personal data.

 name = input("Full Name: ")


national_id = input("National ID: ")
sector = input("Employment Sector: ")
monthly_salary = float(input("Monthly Salary: "))

// Takes input for name, national ID, employment sector, and


Monthly Salary.

 monthly_salary = float(input("Monthly Salary: "))

// Takes the monthly salary and converts it to a float (decimal


number).

 return {
"name": name,
"national_id": national_id,
"sector": sector,
"monthly_salary": monthly_salary
}

// Returns a dictionary containing all the collected user data.

 Function: get_loan_details():
def get_loan_details():

// Defines a function to collect loan details from the user.

 print("\nLoan Details:")

// Displays a header for the loan details section.

 loan_amount = float(input("Desired Loan Amount: "))

// Takes the desired loan amount and converts it to a float.

 print("Choose Loan Duration (months): 12, 24, 36, 48, 60")

// Shows the allowed loan durations.

 while True:
duration = int(input("Loan Duration (months): "))
if duration in [12, 24, 36, 48, 60]:
break
else:
print("Invalid duration. Please choose 12, 24, 36, 48, or 60
months.")

// Continuously prompts the user until they enter a valid loan


duration.

 return loan_amount, duration


// Returns the loan amount and duration.

 Function: calculate_simple_interest()

def calculate_simple_interest(loan_amount, duration,


annual_interest_rate):

// Defines a function to calculate simple interest.


 total_interest = loan_amount * (annual_interest_rate / 100) *
(duration / 12)

// Calculates the total interest using the simple interest formula.

 total_amount = loan_amount + total_interest


 monthly_payment = total_amount / duration

// Calculates the total amount to repay and the monthly


installment.

 return monthly_payment, total_interest

// Returns the monthly payment and total interest.

 Function: calculate_compound_interest()

def calculate_compound_interest(loan_amount, duration,


annual_interest_rate):

// Defines a function to calculate compound interest.

 monthly_rate = annual_interest_rate / 12 / 100

// Converts annual interest rate to monthly decimal rate.

 total_amount = loan_amount * ((1 + monthly_rate)^duration)

// Applies the compound interest formula.

 total_interest = total_amount - loan_amount


monthly_payment = total_amount / duration

// Calculates total interest and monthly payment.

 return monthly_payment, total_interest

// Returns the monthly payment and total interest.


Function: main()
 def main():
// Defines the main function to execute the overall program logic.
 borrowers = []
// Initializes an empty list to store data for all borrowers.

 num_borrowers = int(input("Enter the number of borrowers: "))


 annual_interest_rate = float(input("Enter the annual interest rate
(%): "))

// Collects the number of borrowers and the annual interest rate.

 print("Choose interest type:")


print("1. Simple Interest")
print("2. Compound Interest")

// Prompts user to choose between interest types.

 while True:
interest_choice = input("Enter 1 or 2: ")
if interest_choice in ['1', '2']:
break
else:
print("Invalid choice. Please enter 1 or 2.")

// Validates the interest type input.

 for i in range(num_borrowers):

// Loops through each borrower.

 print(f"\n--- Borrower {i+1} ---")


user_data = collect_user_data()
loan_amount, duration = get_loan_details()

// Displays borrower index, collects personal and loan details.

 if interest_choice == '1':
monthly_payment, total_interest =
calculate_simple_interest(loan_amount, duration, annual_interest_rate)
interest_type = "Simple"
else:
monthly_payment, total_interest =
calculate_compound_interest(loan_amount, duration,
annual_interest_rate)
interest_type = "Compound"

// Calculates interest based on chosen method.

 borrower_info = {
"user_data": user_data,
"loan_amount": loan_amount,
"duration": duration,
"monthly_payment": monthly_payment,
"total_interest": total_interest,
"annual_interest_rate": annual_interest_rate,
"interest_type": interest_type
}

// Organizes all the borrower's data in a dictionary.

 borrowers.append(borrower_info)

// Adds this borrower's data to the list.

 Print Loan Summaries

print("\n*** Loan Summaries for All Borrowers ***")

// Displays header for loan summary section.

 for idx, borrower in enumerate(borrowers, 1):

// Loops through each borrower for summary display.

print(f"\n--- Borrower {idx} ---")


print(f"Name: {borrower['user_data']['name']}")
print(f"National ID: {borrower['user_data']['national_id']}")
print(f"Sector: {borrower['user_data']['sector']}")
print(f"Monthly Salary: {borrower['user_data']
['monthly_salary']:.2f}")
print(f"Loan Amount: {borrower['loan_amount']:.2f}")
print(f"Loan Duration: {borrower['duration']} months")
print(f"Annual Interest Rate: {borrower['annual_interest_rate']}
%")
print(f"Interest Type: {borrower['interest_type']}")
print(f"Total Interest: {borrower['total_interest']:.2f}")
print(f"Monthly Installment (including interest):
{borrower['monthly_payment']:.2f}")

// All above to Prints formatted loan and personal info for each
borrower.

 Execution Entry Point

if __name__ == "__main__":

main()

// Ensures the main() function runs only when the script is executed
directly, not when imported as a module.

You might also like