月用Python 3还清信用卡的预期输出错误

时间:2019-02-17 22:56:54

标签: python python-3.x

我正在做here中列出的相同问题,但是在python 3中却没有得到确切的预期结果,因此我的主要公式不正确。我尝试了一些尝试,但是当我这样做时会产生ValueError数学域错误,具体取决于是否使用括号是方程式的某些部分。我尝试查看另一个存在相同问题但没有运气的线程。在此先感谢!

这一个。 num_of_months =(-1/30)*(math.log(1 +((余额/每月付款))*(1-(((1 +每日费用)** 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.
"""

1 个答案:

答案 0 :(得分:1)

错误和错误的计算是由您的公式引起的

APR应该为float

  

apr = int(math.ceil(float(input(“卡上的APR是多少?   百分)? “))))

因此,如果您输入0.12并强制转换intmath.ceil(),它将返回1 reference

我将计算结果进行了拆分,以得到更好的概述:)

提示:在考虑用户输入之前,请先将计算结果拆分为概述,然后使用固定的数字输入进行测试。

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.
"""