2014-02-11 96 views
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() 

當這個程序運行時,monthlyRate變量(在printPayments中定義)迭代,但calculateValues中計算的值不會迭代。我發現我需要給monthlyRate計算值作爲參數,但我不確定如何做到這一點,沒有一切都打破。迭代的Python問題

+0

提醒一下 - 如果答案很好地解決了你的問題,你應該正式接受它。祝你好運! – kbshimmyo

回答

0

這是容易的,如果你的代碼是因爲你已經輸入了有你的return語句縮進太多

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 

它應該看起來像上面你是第一次迭代之後返回

我只是在這裏猜測你想要每次迭代的結果,你需要將它們保存在某個容器中。我個人會將它們保存在字典中,但沒有準備好,直到您的目標更清晰爲止

您應該考慮一件事,直到您更舒適爲止,可以在函數中添加一些打印語句,以便您可以查看數值每次迭代

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

y = testFunction(2) 

hello 
hello 
>>>y 
1 
+0

我已經試過這個,它不能解決任何問題。 – user3295439

+0

您是否說您的代碼已格式化,因爲我已將其格式化爲 – PyNEwbie

+0

您應該更清楚您是否在尋找每筆每月付款 – PyNEwbie

0

問題是,您的代碼第一次遇到「返回」語句時,它將返回值並離開函數。看起來你正在用printRayments循環monthlyRate(monthlyRate = [4,5,6,7,8])。沒關係。現在對於您的calculateValues函數,您希望將monthlyPayment和totalPayment返回給調用函數。您還需要將monthlyRate作爲參數傳入。

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)) 

一般情況下,它看起來像你需要做一堆的閱讀和思考如何寫在計算機編程功能,特別是其中一個變量可以在程序的執行過程中看到。下面是這種閱讀可能有用的鏈接:

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

希望這有助於。

+0

這工作。非常感謝你 – user3295439

2

代碼中有幾個問題,您可以通過使用下面的代碼執行比較來輕鬆查看。

  1. calculateValues:循環運行,並返回在第一次循環
  2. printPayments調用calculateValues但不拿起結果(這就是爲什麼在同一行再次印刷,再
  3. printPayments結果也打印在一個循環中 - 這意味着有一個不需要的循環
  4. main()調用calculateValues然後printPayments - 這意味着兩個for循環再次調用的次數太多而不保存結果

這裏的固定碼:

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): 

我覺得這事是錯在你算算這裏的利益的方式:

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

但這部分我會讓你自己解決;)

+0

我試過你的代碼,它沒有按預期工作。我也不確定你改變了什麼。 – user3295439

+0

查看更新的答案 – alfasin