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