我正在做同样的问题列出here但在python 3并没有得到确切的预期结果所以我的主要公式是关闭。我尝试了一些东西,但是当我这样做时会产生一个ValueError数学域错误,这取决于我是否使用括号是等式的某些部分。我试着查看另一个有相同问题但没有运气的线程。提前致谢!!!
这个。 num_of_months =(-1/30)*(math.log(1 +((balance / monnthly_payment))*(1 - ((1 + daily_rate)** 30)))/ math.log(1 + daily_rate));
Months to Pay Off a Credit Card problem
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.
"""
错误和错误的计算是由您的公式引起的
APR应该是float
:
apr = int(math.ceil(float(输入(“卡上的APR是什么(百分比)?”))))
因此,如果你输入qazxsw poi并施放qazxsw poi和qazxsw poi,它将返回0.12
int
我拆分计算以获得更好的概述:)
Tipp:在考虑用户输入之前,使用固定的数字输入拆分您的计算概述和测试。
math.ceil()