2015-09-06 42 views
0

的問題是價值找到你需要的時候,要到一家信用卡公司的固定金額 -關於不改變N

  • BAL =量,你需要在第0個月
  • 年初支付
  • N =但是要支付給信用卡公司每月固定金額,使得在今年年底,你將支付的總金額信用卡公司
  • INT =利率
    bal = int(raw_input("Enter balance")) 
    rate = int(raw_input("enter rate")) 
    lower_b = bal/12 
    upper_b = (bal + ((rate*bal)/1200))/12 
    N= (lower_b+upper_b)/2 
    def Credit(bal,rate,N): 
         global upper_b 
         global lower_b 
         i=1 
         k=bal 
         while (i<13): 
          print(N) 
          paid = N 
          bal = bal - paid 
          print("Balance remains to be paid is %s" %(round(bal,2))) 
          Interest = rate * bal /1200 
          print("The interest added on is %s" %(round(Interest,2))) 
          bal=bal+Interest 
          print ("The amount that needs to be payed is %s " %(round(bal,2))) 
          i=i+1 
    
         if bal==0: 
          return N 
         elif 50 < bal < 2000 : 
          lower_b = N 
          upper_b = upper_b 
          N = (upper_b +lower_b)/2 
          return Credit(k,rate,N) 
         elif -2000<bal< -50: 
          upper_b = N 
          lower_b = lower_b 
          N = (lower_b +upper_b)/2 
          return Credit(k,rate,N) 
         elif -50 < bal < 50: 
          return N 
         else: 
          return bal 
    
    
    result=Credit(bal,rate,N) 
    print(result) 
    

    我的代碼永遠不會終止。問題是代碼中定義的N值沒有變化。它仍然是固定的N = upper_b + lower_b)/ 2

  • +0

    這不是一個很好的使用情況遞歸 –

    +1

    都會響起@PadraicCunningham評論,我不會用遞歸,而不是使用全局變量。我認爲這是你被絆倒的地方。從用戶處獲取輸入,並將其發送給某個功能。你可能會得到一個包裝函數,但有時候比使用全局變量遞歸更容易。 –

    回答

    0

    使用遞歸不是理想的方法,你也有邏輯錯誤,包括需要得到月份的利率,你的初始上限應該大於委託人加上興趣。您可以使用while循環與內部的for循環每個不成功內環復位後的餘額:

    balance = int(raw_input("Enter balance")) 
    int_rate = float(raw_input("enter rate")) 
    int_rate /= 100 
    lower_b = balance/12. 
    upper_b = ((balance * (1 + (int_rate/12.0)) ** 12)/12.0) 
    payment = (lower_b + upper_b)/2 
    def Credit(bal, rate, low, high, pay): 
        new_b = bal 
        # calculate monthly interest rate 
        month_int = rate/12 
        while abs(new_b) > 0.001: # use epsilon 
         # always reset balance 
         new_b = bal 
         for _ in range(12): # loop over 12 month range 
          new_b -= pay # deduct pay 
          new_b += month_int * new_b 
         # if we still have a balance we need to take more so set low to current payment 
         if new_b > 0: 
          low = pay 
         # else we took to much so set high to current payment 
         else: 
          high = pay 
         pay = (low + high)/2.0 
        return "Lowest monthly payment over 12 months: {}".format(pay) 
    
    
    print(Credit(balance, int_rate, lower_b, upper_b, payment)) 
    
    +1

    我已經做出了或多或少和你一樣的程序..'bal = int(raw_input(「」)) rate = float(raw_input(「」)) up =(bal + bal * rate)/ 12 print向上 低= BAL /付費12 =(高達+低)/ 2 打印支付 DEF信用(BAL,速率,向上,低,支付): new_b = BAL 而ABS(new_b)> 100: new_b = BAL 對於i在範圍(12): new_b = new_b +(new_b *率/ 12) new_b = new_b - 支付 如果(new_b> 0): 向上=支付 否則: 低=支付 付費=(up + low)/2.0 退貨支付 print信用(支付,費率,上限,下限,支付)' –