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计算为值作为参数,但我不确定如何在没有一切破坏的情况下做到这一点。
答案 0 :(得分:2)
代码中存在一些问题,您可以通过以下代码执行diff来轻松查看。
这是固定代码:
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()
<强>输出:强>
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))
但是这部分我会让你自己解决;)
答案 1 :(得分:0)
如果你的代码就像你在那里键入的那样你的返回语句缩进太多
,这很容易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
答案 2 :(得分:0)
问题是你的代码第一次遇到“return”语句时,它会返回值并离开函数。看起来你在printpayments中循环遍历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
希望这有帮助。