2014-02-26 23 views
0

我正在創建一個程序,它將計算出信用卡支付的最低月度支付,並給定一定的信用卡餘額和利率。回報的時間框架是12個月,每月付款必須精確到最近的一分錢,使用二分搜索。無法得到此while while循環權(python)

我能夠得到答案,問題是我無法讓我的while循環退出,一旦每月支付計算到最接近的分,所以我不得不做一個無限的while循環,有一個elif聲明在while循環的底部,爲我退出。我想知道是否有人可以找出給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 
+1

你必須使用bisect?分析答案是可能的。 –

+0

但是......你不是已經有這個條件嗎?只要'while balance_tested <= 0.01:' – yuvi

+0

不得不使用平分線,因爲這個特殊的問題要求我 – electronicaneer

回答

1

是否有任何理由使用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 
0

你已經有了條件,爲什麼不把對於while語句呢?

while not balance_tested <= 0.01: 
    # etc. 
+0

這是休息的條件,所以你想要相反。 – Anov

+0

沒錯。修復它 – yuvi

0
while abs(balance_tested != 0): 

會留在循環,如果balance_tested變爲負值。做這個

while balance_tested >= 0.1: 

這將自動處理舍入時,它跌到一分錢以下。

+0

哦哇我想我只是厭倦了/寫太多,我只是沒有看到它。我只是使用平分搜索,因爲我正在做一個教程,要求我這樣做。你有沒有看到我編寫的程序可能會讓它變得更好? – electronicaneer

+0

@ nolls1當設置高和低時,我會對高或低的值進行if測試,而不是balance_tested。那就是如果montlyPayment> high:high = monthlyPayment – sabbahillel

+0

請忽略之前的評論。我錯誤地使用了高和低的幾個月來的最高和最低。但是,如果測試重做高和低是錯誤的,因爲當balance_test <0時,您應該重做monthlyPayment作爲實際餘數並從每月循環中退出(然後離開外循環)並打印。此外,您應該重新計算每個月的低位和高位,以balance_tested /剩餘的月數和高位爲balance_tested受剩餘月份的影響。最後的計算(12個月)的分母是1,這樣低的支付貸款(如果你想) – sabbahillel