用Python 3支付信用卡的月数预期输出错误
我在做同样的问题,但在Python3中没有得到确切的预期结果,所以我的主要公式是关闭的。我尝试了一些事情,但当我这样做时,它会创建一个ValueError数学域错误,这取决于我是否使用括号是等式的某些部分。我尝试查看另一个有相同问题但没有运气的线程。提前感谢 这个。月数=-1/30*math.log1+余额/周一付款*1-1+日费率**30 /math.log1+每日费率用Python 3支付信用卡的月数预期输出错误,python,python-3.x,Python,Python 3.x,我在做同样的问题,但在Python3中没有得到确切的预期结果,所以我的主要公式是关闭的。我尝试了一些事情,但当我这样做时,它会创建一个ValueError数学域错误,这取决于我是否使用括号是等式的某些部分。我尝试查看另一个有相同问题但没有运气的线程。提前感谢 这个。月数=-1/30*math.log1+余额/周一付款*1-1+日费率**30 /math.log1+每日费率 错误和错误的计算是由您的公式引起的 APR应该是浮动的: apr=intmath.ceilfloatinput卡上的apr作
错误和错误的计算是由您的公式引起的 APR应该是浮动的: apr=intmath.ceilfloatinput卡上的apr作为 百分比 因此,如果输入0.12并强制转换int和math.ceil,它将返回1 我对计算进行了拆分,以获得更好的概述:
TIPP:在考虑用户输入之前,将您的Calc进行概览并用固定的数字输入进行测试。
我在你的代码中看到了一个分号:D,这通常不是一个好符号。daily_rate=apr/100/365作为百分比添加刚刚意识到我没有正确完成这部分,因为它是一个百分比,并且忘记了除以100。谢谢
import os
import math
os.system('cls')
def calculateMonthsUntilPaidOff(bal,apr,monthlyPayment):
balance = bal
daily_rate = apr / 365
monnthly_payment = monthlyPayment
num_of_months = (-1/30) * (math.log(1 + (balance/monnthly_payment)) * (1 - ((1 + daily_rate)**30))
/math.log(1 + daily_rate))
return num_of_months
balance = int(input("What is your balance? "))
apr = int(math.ceil(float(input("What is the APR on the card (as a percent)? "))))
monnthly_payment = int(input("What is the monthly payment you can make? "))
months_to_pay_off = calculateMonthsUntilPaidOff(balance,apr,monnthly_payment)
print(f"It will take you {months_to_pay_off} months to pay off this card.")
"""
Test Results:
What is your balance? 5000
What is the APR on the card (as a percent)? 12
What is the monthly payment you can make? 100
It will take you 6.640964973685612 monhts to pay off this card.
Expected Results:
What is your balance? 5000
What is the APR on the card (as a percent)? 12
What is the monthly payment you can make? 100
It will take you 70 months to pay off this card.
"""
import os
import math
os.system('cls')
def calculateMonthsUntilPaidOff(bal,apr,monthlyPayment):
balance = bal
daily_rate = apr / 100 / 365 # added as percentage
monnthly_payment = monthlyPayment
r1 = -1/30
r2 = math.log(1+ (balance/monnthly_payment) * (1- math.pow((1 + (daily_rate)), 30)))
r3 = math.log(1+daily_rate)
return r1 * (r2/r3)
balance = int(input("What is your balance? "))
apr = int(input("What is the APR on the card (as a percent)? "))
monnthly_payment = int(input("What is the monthly payment you can make? "))
months_to_pay_off = months_to_pay_off = math.ceil(calculateMonthsUntilPaidOff(balance,apr,monnthly_payment))
print(f"It will take you {months_to_pay_off} monhts to pay off this card.")
"""
Test Results:
What is your balance? 5000
What is the APR on the card (as a percent)? 12
What is the monthly payment you can make? 100
It will take you 70 monhts to pay off this card.
Expected Results:
What is your balance? 5000
What is the APR on the card (as a percent)? 12
What is the monthly payment you can make? 100
It will take you 70 months to pay off this card.
"""