我必须创建一个类,并在其中创建3个方法。我们得到了一堆断言检查,我似乎无法超越第一个。 此方法不会通过,因此我确定其他两个也不会(其他两个使用我从第一个中获得的值)。我错过了什么吗?
class Loan(object):
"""Calculates information regarding Loans."""
def __init__(self,principal,rate,time):
self.principal = principal
self.rate = rate
self.time = time
def calculate_monthly_payment(self):
"""Calculates monthly loan payments"""
part_1 = self.rate*((1+self.rate)**self.time)
part_2 = ((1+self.rate)**self.time)-1
monthly_pmt = self.principal*(part_1//part_2)
return monthly_pmt
我得到了断言检查:
# Testing Loan
loan1 = Loan(100, 0.1, 10)
assert math.isclose(loan1.principal, 100, abs_tol=0.00001), "{} != {}".format(loan1.principal, 100)
assert math.isclose(loan1.rate, 0.1, abs_tol=0.00001), "{} != {}".format(loan1.rate, 0.1)
assert math.isclose(loan1.time, 10, abs_tol=0.00001), "{} != {}".format(loan1.time, 10)
assert math.isclose(loan1.calculate_monthly_payment(), 16.274539488251154, abs_tol=0.00001), "{} != {}".format(loan1.calculate_monthly_payment(), 16.274539488251154)
断言错误:
File "main.py", line 161, in main
assert math.isclose(loan1.calculate_monthly_payment(), 16.274539488251154, abs_tol=0.00001), "{} != {}".format(loan1.calculate_monthly_payment(), 16.274539488251154)
AssertionError: 0.0 != 16.274539488251154
答案 0 :(得分:1)
您使用的是整数除法(//
),您要在倒数第二行中使用浮点除法(/
)。 part1 // part2
舍入为零,因此您返回零,这是不正确的。