Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/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
Can';无法正确执行while循环(python)_Python - Fatal编程技术网

Can';无法正确执行while循环(python)

Can';无法正确执行while循环(python),python,Python,我正在创建一个程序,在给定一定的信用卡余额和利率的情况下,计算信用卡每月最低付款额。支付的时间框架为12个月,每月支付必须精确到最接近的便士,使用二等分搜索 我能够得到答案,问题是,我无法让我的while循环退出,一旦每月付款计算到最接近的美分,所以我必须做一个无限while循环,在while循环的底部有一个elif语句,为我退出。我想知道是否有人能想出给while循环什么条件,让它自己退出。另外,我一周前才开始学习python,希望得到一些关于代码好坏的建议。有什么想法吗 # random b

我正在创建一个程序,在给定一定的信用卡余额和利率的情况下,计算信用卡每月最低付款额。支付的时间框架为12个月,每月支付必须精确到最接近的便士,使用二等分搜索

我能够得到答案,问题是,我无法让我的while循环退出,一旦每月付款计算到最接近的美分,所以我必须做一个无限while循环,在while循环的底部有一个elif语句,为我退出。我想知道是否有人能想出给while循环什么条件,让它自己退出。另外,我一周前才开始学习python,希望得到一些关于代码好坏的建议。有什么想法吗

# random balance
balance = 999999
# random interest rate
annualInterestRate = 0.18 
# assign balance to another variable that will undergo the testing
balance_tested = balance
# bounds of bisection search
low = (balance / 12.0) 
high = ((balance * (1 + (annualInterestRate/12.0))**12)/12.0)
# start month
month = 1
monthlyPayment = (low + high) / 2.0 #Averages out the bounds to meet in the middle
while abs(balance_tested != 0): #While loop that I can't get right, just made it to run infinitely
    balance_tested = balance #Resets balance being tested back to original balance
    monthlyPayment = (low + high) / 2.0 #Bisection search recalculates
    month = 1 #Month reset back to 1
    while month <= 12: #Loops through all 12 months with the payments being made and interested getting added
        balance_tested = (balance_tested - monthlyPayment)
        balance_tested += (balance_tested * (annualInterestRate/12))
        month += 1
        print "Balance Remaining: %.20f" % balance_tested
    if balance_tested < 0: #If the bisection search guesses to high, decreases the high bound
        high = monthlyPayment
    elif balance_tested <= 0.01: #Conditional statement that stops the testing if the balance gets paid off to the cent
        break
    else: #If bisection search guesses to low, increases low bound
        low = monthlyPayment
    print "Monthly Payment: %.2f" % monthlyPayment

print "Lowest Payment: %.2f" % monthlyPayment
#随机平衡
余额=999999
#随机利率
年利率=0.18
#将天平分配给将要进行测试的另一个变量
平衡测试=平衡
#二分搜索的界
低=(余额/12.0)
高=((余额*(1+(年度利率/12.0))**12)/12.0)
#开始月份
月份=1
月支付=(低+高)/ 2α平均出满足在中间的界限。
虽然abs(平衡测试!=0):#虽然我不能正确地进行循环,但它可以无限地运行
balance_tested=balance#将被测天平重置为原始天平
monthlyPayment=(低+高)/2.0#二等分搜索重新计算
月份=1#月份重置回1

whilemonth是否有任何理由使用break语句而不是将其作为while循环的条件

balance = 999999 
annualInterestRate = 0.18 
balance_tested = balance
low = (balance / 12.0) #Lower bound of bisection search
high = ((balance * (1 + (annualInterestRate/12.0))**12)/12.0) 
month = 1 
monthlyPayment = (low + high) / 2.0 
while not (balance_tested <= 0.01): 
    balance_tested = balance 
    monthlyPayment = (low + high) / 2.0 
    month = 1 
    while month <= 12: 
        balance_tested = (balance_tested - monthlyPayment)
        balance_tested += (balance_tested * (annualInterestRate/12))
        month += 1
        print "Balance Remaining: %.20f" % balance_tested
    if balance_tested < 0: 
        high = monthlyPayment
    else: 
        low = monthlyPayment
    print "Monthly Payment: %.2f" % monthlyPayment

print "Lowest Payment: %.2f" % monthlyPayment
balance=999999
年利率=0.18
平衡测试=平衡
low=(balance/12.0)#对分搜索的下界
高=((余额*(1+(年度利率/12.0))**12)/12.0)
月份=1
月付款=(低+高)/2.0

虽然没有(balance_tested你已经有了这种情况,为什么不把它作为while语句呢

while not balance_tested <= 0.01:
    # etc.
未进行平衡测试时
如果测试的平衡变为负值,将保持在循环中。进行此操作

while balance_tested >= 0.1:

这将自动处理当它下降到一便士以下时的舍入。

你必须使用对分?一个分析答案是可能的。但是…你不是已经有了这个条件吗?只要做
,而balance\u tested必须使用对分,因为这个特殊的问题让我这么做,这是要打破的条件,所以你会想要相反的。哦哇我想我只是太累了/写得太远了,所以我没有看到。我只是在使用二分搜索,因为我正在做一个教程,要求我这样做。你看到我如何编写程序的任何不好的地方吗?当设置高和低时,我会对高或l的值进行if测试低而不是余额测试。即如果monthlyPayment>high:high=monthlyPayment请忽略前面的评论。我错误地理解了您使用的high和low是几个月来的最高值和最低值。但是,如果重做high和low的测试是错误的,因为当余额测试<0时,您应该重做monthlyPayment,使其成为实际剩余值,并且通过打印打破月度循环(然后离开外部循环)。此外,您还应重新计算受剩余月份影响的每月余额测试/每月剩余余额和每月余额测试的上限和下限。最终计算(第12个月)的分母为1,因此,低分母将偿还贷款(如果您愿意)
while balance_tested >= 0.1: