gpt4 book ai didi

python - 对分搜索最低付款 (Python) - 答案太大

转载 作者:行者123 更新时间:2023-12-01 04:10:47 25 4
gpt4 key购买 nike

进行二分搜索以确定一年内还清信用卡余额的最低每月还款额。我的结果总是大了几百,并且无法在MITx:6.00.1x在线提交模块中打印(总是打印0.00)。有人可以提供一些建议吗?

代码:

balance = 999999
annualInterestRate = 0.18
interest = annualInterestRate / 12.0
lower = balance / 12.0
upper = (balance * (1+ interest)**12) / 12.0
guess = (upper + lower) / 2

remainder = balance
newBalance = 0

epsilon = 0.00000001

while (newBalance > epsilon):
guess = (upper + lower) / 2
for i in range (0,12):
newBalance = remainder - guess
remainder -= guess
month_interest = newBalance * interest
remainder = newBalance + month_interest

if newBalance < 0:
upper = guess
newBalance = balance
elif newBalance > epsilon:
lower = guess
newBalance = balance
print "Lowest Payment: %.2f" %guess

最佳答案

问题是,您设置了newBalance = 0。因此 while 条件 newBalance > epsilon 永远不会为真,您会直接跳到 print 语句。将 newBalance 设置为 1 或其他值。

关于python - 对分搜索最低付款 (Python) - 答案太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34977668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com