0
import math
import sys

def calculateValues(loanAmt, numYears):
    for monthlyRate in range (4, 9):
        monthlyRate = monthlyRate / 100
        monthlyPayment = loanAmt * monthlyRate / (1 - math.pow(1 / (1 + monthlyRate), numYears * 12))
        totalPayment = monthlyPayment * numYears * 12
        return monthlyRate, monthlyPayment, totalPayment

def printPayments(monthlyRate, monthlyPayment, totalPayment, loanAmt, numYears):
    print("Rate   Monthly Payment  Total Payment")
    for monthlyRate in range (4, 9):
        calculateValues(loanAmt, numYears)
        print("{0}%     ${1:0.2f}         ${2:0.2f}".format( monthlyRate, monthlyPayment, totalPayment))

def repeat():
    question = str(input("Would you like to create a new table? (Enter y for yes): "))
    if (question == "y"):
        main()
    else:
        sys.exit()

def getPositiveFloat():
    loanAmt = int(input("Enter the amount of the loan: "))
    numYears = int(input("Enter the number of years: "))
    if (loanAmt < 0) or (numYears < 0):
        print("Please enter a positive number for both questions")
        main()
    return loanAmt, numYears

def main():
    loanAmt, numYears = getPositiveFloat()
    monthlyRate, monthlyPayment, totalPayment = calculateValues(loanAmt, numYears)
    printPayments(monthlyRate, monthlyPayment, totalPayment, loanAmt, numYears)
    repeat()

main()

When this program runs, the monthlyRate variable (defined in printPayments) iterates, but the values calculated in calculateValues don't iterate. I've figured out that I need to give monthlyRate to calculateValues as a parameter, but I'm not sure how to do that without everything breaking.

1
  • Just a reminder - if an answer solved your problem well, you should formally accept it. Good luck! Commented Feb 11, 2014 at 14:15

3 Answers 3

2

There are a few problems in the code which you can easily see by doing diff with the following code below.

  1. calculateValues: runs in a loop and returns the result on the first iteration
  2. printPayments calls calculateValues but doesn't pick up the result (which is why the same line is printed again and again
  3. printPayments also prints in a loop - which means there is one loop that is not needed
  4. main() calls calculateValues and then printPayments - which means that the two for-loops are called too many times, again, without saving the result

Here's the fixed code:

import math
import sys

def calculateValues(monthlyRate, loanAmt, numYears):
    monthlyRate = monthlyRate / 100
    monthlyPayment = loanAmt * monthlyRate / (1 - math.pow(1 / (1 + monthlyRate), numYears * 12))
    totalPayment = monthlyPayment * numYears * 12
    return monthlyRate, monthlyPayment, totalPayment

def printPayments(loanAmt, numYears):
    print("Rate   Monthly Payment  Total Payment")
    for monthlyRate in range (4, 9):
        monthlyRate, monthlyPayment, totalPayment = calculateValues(monthlyRate, loanAmt, numYears)
        print("{0}%     ${1:0.2f}         ${2:0.2f}".format(int(monthlyRate*100, monthlyPayment, totalPayment))

def repeat():
    question = str(input("Would you like to create a new table? (Enter y for yes): "))
    if (question == "y"):
        main()
    else:
        sys.exit()

def getPositiveFloat():
    loanAmt = int(input("Enter the amount of the loan: "))
    numYears = int(input("Enter the number of years: "))
    if (loanAmt < 0) or (numYears < 0):
        print("Please enter a positive number for both questions")
        main()
    return loanAmt, numYears

def main():
    loanAmt, numYears = getPositiveFloat()
    printPayments(loanAmt, numYears)
    repeat()

main()

OUTPUT:

Enter the amount of the loan: 100
Enter the number of years: 20
Rate   Monthly Payment  Total Payment
4%     $4.00         $960.08
5%     $5.00         $1200.01
6%     $6.00         $1440.00
7%     $7.00         $1680.00
8%     $8.00         $1920.00
Would you like to create a new table? (Enter y for yes): 

I think that something is wrong in the way you calculate the interest here:

monthlyPayment = loanAmt * monthlyRate / (1 - math.pow(1 / (1 + monthlyRate), numYears * 12))

but this part I'll let you solve by yourself ;)

Sign up to request clarification or add additional context in comments.

1 Comment

I tried your code and it didn't work as intended. I'm also not sure what you changed.
0

The problem is that the first time your code hits a "return" statement, it will return the value and leave the function. Looks like you're looping over monthlyRate (monthlyRate = [4, 5, 6, 7, 8]) in printpayments. That's fine. Now for your calculateValues function, you want to sed monthlyPayment and totalPayment back to the calling function. You'll also need to pass monthlyRate in as a parameter.

def calculateValues(monthlyRate, loanAmt, numYears):
    monthlyRate = monthlyRate / 100
    monthlyPayment = loanAmt * monthlyRate \
                / (1 - math.pow(1 / (1 + monthlyRate), numYears * 12))
    totalPayment = monthlyPayment * numYears * 12
    return (monthlyPayment, totalPayment)

def printPayments(monthlyRate, monthlyPayment, totalPayment, loanAmt, numYears):
    print("Rate   Monthly Payment  Total Payment")
    for monthlyRate in range (4, 9):
        monthlyPayment, totalPayment = calculateValues(monthlyRate, loanAmt, numYears)
        print("{0}%     ${1:0.2f}         ${2:0.2f}".format( monthlyRate, \
               monthlyPayment, totalPayment))

Generally, it looks like you'll need to do a bunch of reading and thinking about how to write functions in computer programming and, particularly, where a variable can be seen during the execution of a program. Here's a potentially useful link for such reading:

http://gettingstartedwithpython.blogspot.com/2012/05/variable-scope.html

Hope this helps.

Comments

0

This is easy if your code is as you have typed up there your return statement is indented too much

def calculateValues(loanAmt, numYears):
    myAnswer = []
    for monthlyRate in range (4, 9):
        monthlyRate = monthlyRate / 100
        monthlyPayment = loanAmt * monthlyRate / (1 - math.pow(1 / (1 + monthlyRate), numYears * 12))
        totalPayment = monthlyPayment * numYears * 12
        thisRound = (monthlyRate, monthlyPayment, totalPayment)
        myAnswer.append(thisRound)
    return myAnswer

It should look something like above you are returning after the first iteration

I am just guessing here that you want the results from each iteration, you need to save them in some container. I personally would save them in a dictionary but not ready to until your goal is more clear

One thing you should consider until you are more comfortable is to add some print statements to your functions so you can see what the values are during each iteration

 def testFunction(somevalue):
    for x in range(0, somevalue):
        print 'hello'
    return x

y = testFunction(2)

hello
hello
>>>y
1

5 Comments

Are you saying that your code is formatted as I have formatted it
You should be more clear is it that you are looking for each of the monthly payments
Yes. Having the code exactly like that does not fix the problem.
Run the program. The monthly and total payments should iterate and they don't.
@user3295439 that's because this is only one out of many problems. See my answer for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.