Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这个计划不会终止?_Python_Loops_While Loop_Terminate - Fatal编程技术网

Python 为什么这个计划不会终止?

Python 为什么这个计划不会终止?,python,loops,while-loop,terminate,Python,Loops,While Loop,Terminate,我试图自学python,但没有编写代码的经验。在我的第一次尝试中,我试图编写一个程序,将滚雪球原则应用于债务减免,但也会在每次付款中增加一个额外的固定金额。我可以让第一笔债务清偿(它变成负数,但退出循环)。我的第二步不会退出循环,我已经研究了处理嵌套循环的主题,但它们没有帮助。有人能告诉我哪里出错了吗 #Temp fixed number for testing use rawinput for actual program. #name the debt debt1 = "CC A" #cu

我试图自学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

看看这个循环:

while backPay >= 0:
    balance2 -= standPay2
    backPay += 1
这里,
backPay
in在每次迭代中都会增加,因此条件
backPay>=0
将始终为真


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

是不是
balance2
曾经小于零?而backPay>=0,backPay+=1如果要开始学习代码,您还需要找到调试的好方法。尝试在您认为可能存在错误的代码的不同部分添加打印语句。从那里检查预期输入是否与实际输出相似。我最初使用乘法路线,但意识到我需要调整,以适应以下事实:在
intPayment2=round(balance2*(annualInterestRate2/12)中,2)
intPayment2
将随着
balance2
的减少而改变
backPay
是我试图建立一个循环,根据
balance2
中不断变化的值进行调整。希望这能让你sense@nsidla1327但无论是
intPayment2
还是
standPay2
都不会在循环内部发生变化,因此循环(使用
backPay-=1
)似乎等同于
balance2-=standPay*(backPay+1)
。不管怎样,很高兴我能帮上忙。