hi everyone, following is a part of my assignment and your help will deeply appreciated. Thank you, I'm a complete newbie at python.

q) each month should:

  1. Calculate the monthly interest payment by multiplying the monthly interest rate, as a fraction, by the remaining principal.

  2. Determine the amount remaining to be paid against the principal by subtracting the interest payment from the monthly payment amount

    Reduce the principal by the amount calculated above.
    Add the monthly interest to the running sum for total interest paid.
    Add the monthly payment to the running sum of the monthly payments.
    Check to see if this is the 12th month and the user has a non-zero anniversary payment percentage. If so, calculate the anniversary payment and subtract the entire amount of this payment from the principal. Add the payment amount to the running sum of total anniversary payments. Display the anniversary payment, if made.
    Display the month and the remaining principal.

following is my messed up code, really need help cleaning this up. Thanks:

# 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

    # Get payment term and form loop to meet condition:
    paymentTerm = int(input("Enter payment term in years: "))

    while paymentTerm < 1 or paymentTerm > 30:
        print("Payment term is not legal. Exiting program!")
        paymentTerm = int(input("Enter payment term in years:"))


    # Get annual interest %:
    annualInterestPercent = float(input("Enter annual interest rate as %: "))

    while annualInterestPercent < 1 or annualInterestPercent >10:
        print("Annual interest rate is not legal. Exiting Program!")
        annualInterestPercent = float(input("Enter annual interest rate as %:"))



    # Get Principal amount and form loop to meet condition:    
    principal = float(input("Enter principal amount in $: "))

    while principal < 1000 or principal > 500000:
        print("Principal amount is not legal. Exiting program!")
        principal = float(input("Enter Principal amount in $:"))

    # Get anniversary payment %:

    anniversaryPaymentPercent = float(input("Enter the anniversary payment percentage:"))

    while anniversaryPaymentPercent < 0 or anniversaryPaymentPercent > 10:
        print ("This is not legal. Exiting Program!")
        anniversaryPaymentPercent = float(input("Enter the anniversary payment percentage:"))


    # Calculate the monthly payment, total interest and anniversary payment:
    termInMonths = 12 * paymentTerm
    monthlyInterestRate = annualInterestPercent / 1200
    monthlyPayment = principal * monthlyInterestRate / (1 - (1 + monthlyInterestRate) ** -termInMonths)
    totalInterest = termInMonths * monthlyPayment - principal
    anniversaryPayment = monthlyPayment * anniversaryPaymentPercent/100
    principle_after_monthlyPayment = principal - monthlyPayment
    monthly_interestPayment = monthlyInterestRate * principle_after_monthlyPayment
    total_monthlyPayment = monthlyPayment - monthly_interestPayment



    #Display the results
    print("Monthly payment is ${0:.2f}".format(monthlyPayment))

    #Ask how to stop when principle reaches 0 and how the principle will change..don't understand the "running sum concept at all" 
    month = 1
    while principal > 0:
        # monthly calculations:
        month = month+1
        print(month)
        print(principle_after_monthlyPayment)

        if anniversaryPaymentPercent > 0 and month%12==0:


            print("Anniversary payment is",format(anniversaryPayment, '.2f'))
            print("principal is", format(principle_after_monthlyPayment))

               #print("Total interest paid is ${0:.2f}".format(totalInterest))

               #print("Total monthly payment is ",format(total_monthlyPayment, '.2f'))




main()

You would send the values to a function and return the amounts.

def payment(principle, monthly_interest_rate, amount_paid):
    """ assumes a payment is made every month so there are no penalties 
    """
    if principle > 0:     ## loan is not paid off
        interest_due = monthly_interest_rate * principle
        if amount_paid < interest_due:
            print "Payment does not cover interest so nothing done"
        else:
            to_principle = amount_paid - interest_due
            if principle-to_principle > 0:
                principle -= to_principle
                print "You paid %9.2f in interest and %9.2f reduced the principle to %9.2f" % \
                      (interest_due, to_principle, principle)
            else:    # more paid than is owed
                print "You paid off the principle of %9.2f and are being returned %9.2f" % \
                      (principle, to_principle-principle)
    else:
        print "Your loan is paid off.  Congratulations"
    return principle

principle = 100000
interest_rate = 0.06/12
for amount in [1000.11, 100, 10000, 500, 100000]:
    principle=payment(principle, interest_rate, amount)

You can also use one function for all input

def get_input(lit, low, high):
    while 1:
        print "-"*50
        print "Enter a number between %9.2f and %9.2f" % (low, high)
        amount=raw_input(lit)
       
        ## catch anything entered that is not a number
        try:
            amt_float=float(amount)
            if low <= amt_float <= high:
                return amt_float
        except:
            print "Enter numbers only"

years=get_input("Enter payment term in years: ", 1, 30)
yearly_interest=get_input("Enter annual interest rate as %: ", 1, 10)
principle=get_input("Enter principal amount in $: ", 1000, 500000)
anniversary=get_input("Enter the anniversary payment percentage:", 0, 10)

Copied with [code] [/code] tags attached (and do you get extra credit for lots of empty lines).

# 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


    # Get payment term and form loop to meet condition:
    paymentTerm = int(input("Enter payment term in years: "))

    while paymentTerm < 1 or paymentTerm > 30:
        print("Payment term is not legal. Exiting program!")
        paymentTerm = int(input("Enter payment term in years:"))



    # Get annual interest %:


    annualInterestPercent = float(input("Enter annual interest rate as %: "))

    while annualInterestPercent < 1 or annualInterestPercent >10:
        print("Annual interest rate is not legal. Exiting Program!")
        annualInterestPercent = float(input("Enter annual interest rate as %:"))



    # Get Principal amount and form loop to meet condition:

    principal = float(input("Enter principal amount in $: "))

    while principal < 1000 or principal > 500000:
        print("Principal amount is not legal. Exiting program!")
        principal = float(input("Enter Principal amount in $:"))
        


    # Get anniversary payment %:

    anniversaryPaymentPercent = float(input("Enter the anniversary payment percentage:"))

    
    while anniversaryPaymentPercent < 0 or anniversaryPaymentPercent > 10:
        print ("This is not legal. Exiting Program!")
        anniversaryPaymentPercent = float(input("Enter the anniversary payment percentage:"))

    
           
        

    # Calculate the monthly payment, total interest and anniversary payment:
    termInMonths = 12 * paymentTerm
    monthlyInterestRate = annualInterestPercent / 1200
    monthlyPayment = principal * monthlyInterestRate / (1 - (1 + monthlyInterestRate) ** -termInMonths)
    totalInterest = termInMonths * monthlyPayment - principal
    anniversaryPayment = monthlyPayment * anniversaryPaymentPercent/100
    principle_after_monthlyPayment = principal - monthlyPayment
    monthly_interestPayment = monthlyInterestRate * principle_after_monthlyPayment
    total_monthlyPayment = monthlyPayment - monthly_interestPayment
    
    
    

    


     
        
    

    #Display the results
    print("Monthly payment is ${0:.2f}".format(monthlyPayment))
    
    
    
    


#Ask how to stop when principle reaches 0 and how the principle will change..don't understand the "running sum concept at all" 
    month = 1
    while principal > 0:
        # monthly calculations:
        month = month+1
        print(month)
        print(principle_after_monthlyPayment)

        if anniversaryPaymentPercent > 0 and month%12==0:
            
                       
            print("Anniversary payment is",format(anniversaryPayment, '.2f'))
            print("principal is", format(principle_after_monthlyPayment))
            
               #print("Total interest paid is ${0:.2f}".format(totalInterest))
              
               #print("Total monthly payment is ",format(total_monthlyPayment, '.2f'))
              

    

main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.