A group of students is arguing about which are better terms for a loan with compound interest—longer time with lower interest, or shorter time with higher interest. They decide to write a program to calculate each total so that they can compare between both the conditions:

principal = 10000
rate = 10.25
time = 5
Amount = principal * (pow((1 + rate / 100), time))
Total = Amount + principal
print("Compound interest is", Amount + principal)
rate = 5.25
time = 10
Amount = principal * (pow((1 + rate / 100), time))
print("Compound interest is", Amount + principal)
Which code should be placed into a function?

(1 point)
Responses

Amount = principal * (pow((1 + rate / 100), time))
Amount = principal * (pow((1 + rate / 100), time))

principal = 10000
principal = 10000

rate = 10.25
rate = 10.25

print("Compound interest is", Amount + principal)
print("Compound interest is", Amount + principal)
Skip to navigation

Previous

1 answer

To make the code more modular and reusable, the calculation for the amount and total should be placed in a function. This would allow you to call the function with different parameters for principal, rate, and time without duplicating code.

Here's how you might structure the function:

def calculate_compound_interest(principal, rate, time):
    Amount = principal * (pow((1 + rate / 100), time))
    Total = Amount + principal
    return Total

# Now you can call the function with different values
principal = 10000

# First scenario
rate1 = 10.25
time1 = 5
total1 = calculate_compound_interest(principal, rate1, time1)
print("Compound interest for rate", rate1, "and time", time1, "is", total1)

# Second scenario
rate2 = 5.25
time2 = 10
total2 = calculate_compound_interest(principal, rate2, time2)
print("Compound interest for rate", rate2, "and time", time2, "is", total2)

In this setup:

  • The entire calculation of Amount and Total is encapsulated in the calculate_compound_interest function.
  • Each scenario can call this function with different parameters to compute and compare the total amount of the loan with compound interest.