A
A
Andrey Karpov2020-08-13 14:13:45
Python
Andrey Karpov, 2020-08-13 14:13:45

How to write a function for counting money spent for the last 7 days (starting with conditionally today)?

I'm preparing a money count calculator, I wrote a function "for today" (attached), but I just can't figure out how to beat the money count for the last 7 days. Need help.

def get_today_stats(self):
        count = 0
        sum_day = len(self.records)
        
        for i in range(0, sum_day):
            if self.records[i].date == dt.date:
                count = self.records[i].amount + count
        return count

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-08-13
@monarch228

week_ago = datetime.date.today() - datetime.timedelta(days=7)
sum([record.amount for record in self.records if record.date >= week_ago])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question