算术没有在第一个字典条目上完成?

时间:2018-03-05 15:48:31

标签: python python-3.x

class creditCard:
    def __init__(self, limit, apr, balance):
        self.limit = limit
        self.apr = apr
        self.balance = balance
        self.charges = {}
        self.payments = {}

    def charge(self, amount, day):
        # Charges the card, and stores the charge and day the charge was made in a dictionary.
        # Returns false if the amount charged is greater than the limit.

        if amount > self.limit - self.balance:
            return False

        self.charges[day] = amount
        self.balance += amount

    def payment(self, amount, day):
        # When a payment is made the payment is stored in the payments dictionary with the corresponding day.
        # Returns false is the payment amount is greater than the balance owed/limit.

        if amount > self.limit - self.balance:
            return False

        self.payments[day] = amount
        self.balance -= amount

    def interest(self, balance):
        # calculates and returns interest

        return (balance * self.apr) / 365

    def days_balance(self, balance_day):
        balance_to_date = 0
        months_interest = 0
        for day in range(1, balance_day+1):
            balance_to_date += self.charges.get(day, 0)
            balance_to_date -= self.payments.get(day, 0)
            months_interest += self.interest(balance_to_date)

            if day % 30 == 0:
                balance_to_date += months_interest
                months_interest = 0


        balance_to_date = round(balance_to_date, 2)

        return "${}".format(balance_to_date)

我应该为我的第二个测试用例返回411.99而由于字典没有应用第一天的兴趣而得到411.89。我无法弄清楚为什么会这样做。任何帮助将不胜感激。以下是我的测试用例。

  • 客户打开信用卡,限额为1k,年利率为35%
  • 客户在开幕当天收取500美元
  • 开业后15天,他支付200美元(余额现在300)
  • 开放后25天他再收取100美元(余额现在为400)
  • 月末的总余额为411.99

    customer1 = creditCard(1000, 0.35, 0)
    customer1.charge(500, 1)
    print(customer1.days_balance(30))
    for i in customer1.charges:
       print(customer1.charges.keys(), customer1.charges.values())
    
    customer2 = creditCard(1000, 0.35, 0)
    customer2.charge(500, 1)
    customer2.payment(200, 15)
    customer2.charge(100, 25)
    print("Customers Balance on Day 26: " + customer2.days_balance(26))
    print("Customers Balance on Day 30 with Interest: " + 
     customer2.days_balance(30))
    

0 个答案:

没有答案