Reputation: 21
I have to create a class, and 3 methods within it. We were given a bunch of assertion checks, and I can't seem to get past the first one. This method doesn't pass, so I'm sure that the other two won't either (the other two use the value that I get from the first one). Have I missed something?
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
Assertion check I was given:
# 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)
Assertion Error:
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
Upvotes: 0
Views: 44
Reputation: 3491
You're using integer division (//
) where you want float division (/
) in your second-to-last line. part1 // part2
is rounded to zero, and so you return zero, which is incorrect.
Upvotes: 1