I would like to execute a portion of script once daily for about 7 days. I used the datetime module in getting the current date, the timedelta module in calculating the runtime of the program, and the localtime() in specifying a particular time the code is to run daily. The first time I executed it, the balance doesn't add up automically. I think it keeps reassigning the balance variable.
Here is a portion of my code.
balance = 0
percentage = 5
deposit = 0
profit = 0
active = True
while active:
if current_date < expiring_date:
balance = deposit + percentage
current_date = current_date + timedelta(hours=24)
if time.localtime().tm_hr == 2:
balance += percentage
else:
active = False
Then, I modified the code by using conditionals to check if the initial balance is 0, to prevent the reassign ing. Currently the code executes at once, which is not what I want. I want it to run daily, and for each run, it should add the percentage to the current balance till the codition evaluates to False. I also want to keep a track of balance, and return the new balance at the end of each day. Is there a better way of doing it without keeping the loop running for days,and consuming power.
while active:
if current_date < expiring_date:
if balance <= 0:
balance = deposit + percentage
current_date = current_date + timedelta(hours=24)
else:
if time.localtime().tm_hr == 2:
balance += percentage
current_date = current_date + timedelta(hours=24)
else:
active = False