Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

PYTHON HELPPPPPP (loops)

I came across with this question and I have been trying to code it but it's so hard since I just started python.

Monthly mortgage payments can be calculated using the formula:

A = (p * i) / (1- (1 + i) **(-n))

Where A is the monthly payment, P is the total amount borrowed (the principal), n is the number of months for repayment and i is the monthly interest rate. You can calculate the monthly interest rate by dividing the annual interest rate ("APR") by 12.

For example, to borrow $30,000 (P) for a 10 year term (n = 120) with an APR interest rate of 6% (i = 0.06 / 12 = 0.005), A calculates to be $333.06 - the monthly payment. The total interest paid over 10 years (always depressing!) = 120 * $333.06 - $30,000 = $9,967.38.

Assume that interest rates are fixed and the APR must lie between 1 and 10% inclusive. The term is always in years and must lie between 1 and 30 years inclusive. Payments are always monthly and the principal must lie between $1,000 and $500,000, inclusive.

Write a program that prompts the user for the three required values and then calculates and displays the monthly payment and the total interest paid over the entire term.

In addition to these three inputs, the program must also take into account anniversary payments. In the "real world" of mortgages, payees can opt to make a once a year single lump sum payment directly against the principal. The rules vary from lender to lender, but for the sake of this assignment let us say that this anniversary payment must be the lesser of $5,000 or 5% of the principal remaining. The lender does not want you to pay the mortgage off too quickly! And, you must complete a full year of payments before you can make an anniversary payment. Rather than prompting every year for the amount of the payment, assume that the payment made will be the maximum possible every year. But, you must first give the user the choice of using anniversary payments or not. If the user says "yes", then apply an anniversary payment each year.

After obtaining these four inputs, you will calculate and display each monthly payment and the principal remaining after the payment. Display anniversary payments, if they are made. You must track the total amount of interest paid, the total of anniversary payments, and the total of all monthly payments. Report the number of months that the mortgage payments have been shortened as a result of making anniversary payments. Report the total cost of the mortgage, which is the sum of all anniversary payments and the total of all mortgage payments.

---------------I am stuck on the part about anniversary payment and how to loop it so that it always chooses the maximum amount that is no greater than 5000 or 0.05 * principal. I was just wondering if you could give me some hints on how to proceed. Also, when I run the code right now, it goes to the negative amount. I am not sure which step is wrong :( Here is my code:

# This program calculates a monthly mortgage payment and the total interest
# paid for user-supplied values of the interest rate, principal and term.

def main():

    # Obtain input parameters from the user and check for legality of each
    paymentTerm = int(input("Enter payment term in years: "))
    while paymentTerm < 1 or paymentTerm > 30 :
        paymentTerm = int(input("Payment term is not legal, please try again: "))
    annualInterestPercent = float(input("Enter annual interest rate as %: "))
    while annualInterestPercent < 1 or annualInterestPercent > 10 :
        annualInterestPercent = float(input("Annual interest rate is not legal, please try again: "))
    principal = float(input("Enter principal amount in $: "))
    while principal < 1000 or principal > 500000 :
        principal = float(input("Principal amount is not legal, please try again: "))
    anniversaryPayment = input("Please enter (y) to apply anniversary payment and (n) otherwise: ")
    if anniversaryPayment == "n" :
        # Calculate the monthly payment
        termInMonths = 12 * paymentTerm
        monthlyInterestRate = annualInterestPercent / 1200
        monthlyPayment = principal * monthlyInterestRate / (1 - (1 + monthlyInterestRate) ** -termInMonths)
        monthlyInterestPayment = principal * monthlyInterestRate
        totalInterest = termInMonths * monthlyPayment - principal
        print(f"Monthly payment is ${monthlyPayment:.2f}")
        while paymentTerm <= 30 :
            termInMonths = termInMonths + 1
            principal = principal - monthlyPayment + monthlyInterestPayment
            print(principal)
        # Display the results
        print(f"Monthly payment is ${monthlyPayment:.2f}")
        print(f"Total interest paid is ${totalInterest:.2f}")
    else :
        # Calculate the monthly payment
        termInMonths = 12 * paymentTerm
        monthlyInterestRate = annualInterestPercent / 1200
        monthlyPayment = principal * monthlyInterestRate / (1 - (1 + monthlyInterestRate) ** -termInMonths)
        monthlyInterestPayment = principal * monthlyInterestRate
        totalInterest = termInMonths * monthlyPayment - principal
        print(f"Monthly payment is ${monthlyPayment:.2f}")
        while termInMonths <= 30 * 12 :
            termInMonths = termInMonths + 1
        while paymentTerm <= 30 :
            paymentTerm = paymentTerm + 1
            principal = principal - monthlyPayment + monthlyInterestPayment
            print(principal)
        anniversaryPayment = 5000 or 0.05 * principal
        print(anniversaryPayment, principal)

main()

fixed code formatting

1 Answer

while paymentTerm <= 30 and principal >= 0 :
            termsInMonths = 12 * paymentTerm
            termsInMonths = termsInMonths + 1
            monthlyInterestPayment = principal * monthlyInterestRate
            principal = principal - monthlyPayment + monthlyInterestPayment
            print(f"\t ${principal:.2f}")
            paymentTerm = paymentTerm + 1
            anniversaryPayment = 5000 or 0.05 * principal
            print(anniversaryPayment, principal)

For this part, I want it to stop every 12 months so that I can calculate anniversary payment, but it only stops on the 12th and does not continue for the rest of the months :( also at every 12th months, the anniversary payment amount is either principal * 0.05 or 5000, if principal * 0.05 is > 5000, then just use 5000.....

Steven Parker
Steven Parker
243,318 Points

I'm confused. The snippet you show above doesn't seem to be part of the original program. Is is something you are adding? I think I would need to see the whole program to evaluate it in comparison to the expected output.

Also please see the Markdown Cheatsheet link at the bottom of the "Add an Answer" section for instructions on how to format posted code. :arrow_heading_down: