I have to create a bankaccount class that has functions such as: deposit, withdraw, getbalance; all that beginner stuff. All very easy and I finished it no problem. However, there is a function called Interest that takes in an interest_rate where I am running into a lot of trouble. To explain, this function is supposed to add interest per the user's request only once a month to the balance of the bankaccount. So let's say I decide to add 5% interest on today's date (Oct 13) to my balance. The function would execute and spit out my new balance. Then let's say I want to add 10% interest the next day (Oct 14th), the function would not execute and would spit out something like "Cannot add interest until Nov.1", which is the first of the next month. On Nov.1, if I tried to add 10% interest, I would succeed and then if on Nov. 2 I tried to add interest again, it would say "Cannot add interest until Dec.1" so on and so forth. I'm having a very hard time with this. Below is the code that I wrote, however it would not only ever execute because the date will always be ahead a month, but it would also always add interest to the balance.
Here's what I have so far, but it's a mess. Anyone have any tips on what I should do? I know it's not inside a function the way it's supposed to be in the assignment but I thought I would figure out the main picture of the program first and then worry about incorporating it into the rest of my class.
from datetime import *
from dateutil.relativedelta import *
rate = 0.10
balance = 1000.00 # i just made up a number for the sake of the code but the
# balance would come from the class itself
balancewInterest = rate*balance
print(balancewInterest)
# this is where I'm having the most trouble
todayDate = date.today()
print(todayDate)
todayDay = todayDate.day
todayMonth = todayDate.month
if todayDay == 1: # if it's the first of the month, just change the month
# not the day
nextMonth = todayDate + relativedelta(months=+1)
else: # if not the 1st of the month, change day to 1, and add month
nextMonth = todayDate + relativedelta(months=+1, days=-(todayDay-1))
print(nextMonth)
difference = (todayDate - nextMonth)
print(difference.days)
if todayDate >= nextMonth: # add interest only when it's the first of the next
# month or greater
interestPaid = rate*balance
print(interestPaid)
else:
print("You can add interest again in " + str(abs(difference.days)) \
+ " days.")