-3

trying to create a function to get the principal amount for a loan using the for loop for the sum of present values

def presentValue(amount, num_months, interest): 
    return (amount)/(1 + interest)**num_months
def getPrincipal(amount, num_months, interest):
    return
    getPrincipal = 0
    for presentValue in range(1,240):
    getPrincipal = getPrincipal + presentValue

something must be wrong here but no errors occur

A = 100
interest = 0.01
num_months = 240
P = getPrincipal(A, num_months, interest)
print(P)

doing print(P) returns None

3
  • 1
    The first line of getPrincipal says "return None and ignore the rest" so that's what it does. Commented Oct 21 at 18:45
  • What is your expected output given the input? Commented Oct 21 at 18:45
  • If you try to run the code as shown in your question, you will get an error - specifically, an IndentationError exception Commented Oct 22 at 5:55

1 Answer 1

3

Your getPrincipal function was not correct. Here I made changes,

  1. Added return statement: The function now returns totalPrincipal

  2. Fixed the loop logic: Now properly iterates through each month from 1 to num_months

  3. Used the presentValue function: Each iteration calculates the present value for that specific month

def presentValue(amount, num_months, interest): 
    return (amount)/(1 + interest)**num_months


def getPrincipal(amount, num_months, interest):
    totalPrincipal = 0
    for month in range(1, num_months + 1):
        pv = presentValue(amount, month, interest)
        totalPrincipal = totalPrincipal + pv
    return totalPrincipal

A = 100
interest = 0.01
num_months = 240
P = getPrincipal(A, num_months, interest)
print(P)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.