使用二分搜索,编写一个程序,计算在 12 个月内还清信用卡余额所需的最低固定月供

问题描述

请帮忙...我的代码返回错误输出。我尝试通过多种方式对其进行调试,但都无济于事。

-- 编码:utf-8 --

""" 创建于 2021 年 6 月 17 日星期四 14:56:19

编写一个程序,计算在 12 个月内还清信用卡余额所需的最低固定月供。 """

enter code here

balance = 320000
newbalance = balance
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12
month = 0
Lower = balance / 12
Upper = (balance * (1+ monthlyInterestRate)**12)/12.0                      
monthlyPayment = 0.01
middle = (Lower + Upper)/2 

while(month<=12):    
    middle = (Lower + Upper)/2    
    monthlyOutstanding = newbalance - middle
    updatedBalance = monthlyOutstanding + (monthlyInterestRate*monthlyOutstanding)
    newbalance = updatedBalance
    month+=1 

    if  newbalance < 0 and newbalance < -monthlyPayment:
        
        Upper = middle            
        monthlyPayment+=0.01
        newbalance = balance
    
                    
    if  newbalance > 0 and newbalance > monthlyPayment:
        Lower = middle
        monthlyPayment+=0.01
        newbalance = balance
        
                    
    else:
        print('Lowest payment: ',str(round(middle,2)))
        break

middle = (Lower + Upper)/2 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)