下面的代码不起作用,我非常沮丧。我知道正确的输出应该是310美元但不知何故我的代码还没有到达那里。这是CS和python的edex课程介绍的作业。我试图评论我认为代码正在做什么,但显然我不对。
非常感谢任何帮助或提示。
balance = 3329
annualInterestRate = 0.2
monthInterest = annualInterestRate/12.0
# simple payment for the month
monthlyPaymentRate = 10
# while this is true, run the for loop from 1 - 12. This loop will break if the balance gets < 0, otherwise the
# monthly payment rate adds by 10 each year.
while True:
for month in range(1, 13):
balance = balance - monthlyPaymentRate
interestBalance = balance + (monthInterest*balance)
balance = interestBalance
if balance < 0:
break
else:
monthlyPaymentRate += 10
print "balance = " + str(balance)
print "annualInterestRate = " + str(annualInterestRate)
print"Lowest payment: " + str(monthlyPaymentRate)
答案 0 :(得分:1)
非常感谢您的评论,我能够调整我的代码以获得适当的结果,通过创建一个函数,这意味着我可以测试一年中每月付款率的结果,返回结果如果那个结果不是我想要的,那么会在while循环中重新运行代码。这个学习编码很棘手,但非常有趣。
对清洁度或效率的任何想法都是最受欢迎的,相当肯定这不是最有效的步骤。
balance = 4400
annualInterestRate = 0.18
monthInterest = annualInterestRate/12.0
# simple payment for the month
monthlyPaymentRate = 0
# while the balance is greater than zero, run the for loop from 1 - 12. This loop will break if
# the balance gets <=0, otherwise the monthly payment rate adds by 10 each year.
while balance > 0:
monthlyPaymentRate += 10
def testBalance(balance, monthlyPaymentRate):
for month in range(1, 13):
balance = balance - monthlyPaymentRate
interestBalance = balance + (monthInterest*balance)
balance = interestBalance
return balance
if testBalance(balance, monthlyPaymentRate) <= 0:
break
print"Lowest Payment: " + str(monthlyPaymentRate)