【问题标题】:why wont this program terminate?为什么这个程序不会终止?
【发布时间】:2014-01-08 16:49:49
【问题描述】:

我正在尝试自学 Python,但没有编写代码的经验。对于我的第一次尝试,我正在尝试编写一个程序,将滚雪球原理应用于债务减免,但还会在每次付款时增加额外的固定金额。我可以清除第一笔债务(它变成负数,但它退出了循环)。我的第二步不会退出循环,我查看了处理嵌套循环的主题,但它们没有帮助。有人可以告诉我我哪里出错了吗?

#Temp fixed number for testing use rawinput for actual program.

#name the debt
debt1 = "CC A"
#currnet balnace
balance1 = float(5000)
#APR
annualInterestRate1 = float(.1499)
#Currnet Monthly Payment
minMonthlyPayment1 = float(200)
# Exta Payment
boosterPayment = float(337)

print "The balance on ",debt1," is ",balance1

debt2 = "CC B"
balance2 = float(1000)
annualInterestRate2 = float(.1499)
minMonthlyPayment2 = float(200)

print "The balance on ",debt2," is ",balance2

debt3 = "ICCU"
balance3 = float(6000)
annualInterestRate3 = float(.0879)
minMonthlyPayment3 = float(130)

print "The balance on ",debt3," is ",balance3

debt4 = "Car"
balance4 = float(8000)
annualInterestRate4 = float(.0699)
minMonthlyPayment4 = float(200)

print "The balance on ",debt4," is ",balance4

debt5 = "Truck"
balance5 = float(15000)
annualInterestRate5 = float(.0439)
minMonthlyPayment5 = float(333)

#nubmer of payments made durning the debt reduction. Used as the index.
numPay = 0
save = 0

#For Debt1 with an APR greater then 0
intPayment1 = round(balance1*(annualInterestRate1/12),2)

while balance1 >= 0:
    #payment with intrest
    payment1 = minMonthlyPayment1 - intPayment1 + boosterPayment
    #subtact payment from balance
    balance1 -= payment1
    #count Number of payments
    numPay += 1
print numPay
print balance1

#For Debt2 with an APR greater then 0

#Figures monthly charge based on given APR
intPayment2 = round(balance2*(annualInterestRate2/12),2)
#Monthly payment minus intrest
standPay2 = minMonthlyPayment2 - intPayment2

while balance2 >= 0:
    #payment while debt1 is being paid

    #need a way to pay the payments while the other debt is being figured
    backPay = numPay
    while backPay >= 0:
        balance2 -= standPay2
        backPay += 1
    #payment with intrest takes 100 away for savings
    payment2 = minMonthlyPayment2 - intPayment2 + (boosterPayment-100)
    #subtact payment from balance
    balance2 -= payment2
    #count Number of payments
    numPay += 1
    #keep track of how much is going to savings
    save += 100
print numPay
print balance1
print save

【问题讨论】:

  • balance2 是否曾经小于零?
  • 当 backPay >= 0, backPay += 1
  • 如果您要开始学习代码,您还需要找到调试的好方法。尝试在您认为可能存在错误的代码的不同部分添加打印语句。从那里检查预期输入是否与实际输出相似。

标签: python loops while-loop terminate


【解决方案1】:

看看这个循环:

while backPay >= 0:
    balance2 -= standPay2
    backPay += 1

这里,backPay 在每次迭代中都会增加,因此条件 backPay >= 0 将始终为真。

不确定代码的用途,但可能您必须改为使用backPay -= 1。但是请注意,由于循环的迭代次数是预先知道的,并且您只是在每次迭代中添加一个固定数字,因此您也可以将循环替换为简单的乘法。

【讨论】:

  • 我最初选择乘法路线,但意识到我需要调整 intPayment2 = round(balance2*(annualInterestRate2/12),2) intPayment2 将随着 balance2 减少而变化。 backPay 是我尝试创建一个循环来调整 balance2 中的变化值。希望这是有道理的
  • @nsidla1327 但是intPayment2standPay2 都没有改变循环内部,所以循环(带有backPay -= 1)似乎等同于balance2 -= standPay * (backPay + 1)。无论如何,很高兴我能提供帮助。
猜你喜欢
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
相关资源
最近更新 更多