微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用二分搜索,编写一个程序,计算在 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 举报,一经查实,本站将立刻删除。