I am trying to loop between two times, from 8:00 to 17:00 for every 15 mins
The expected output will be a list of times like
[8:00, 8:15, 8:30, 8:45, 9:00]
This is so far I got
now = datetime(2013, 2, 9, 8, 00)
end = now + timedelta(hours=9)
But I can't figure out how to run the loop to return me my desired list.
Thanks for looking.
To loop through a set of code a certain number of times, you can use the range() function, which returns a list of numbers starting from 0 to the specified end number. You haven't learned about functions yet, but you will soon!
You mean this?
>>> now = datetime(2013,2,9,8,0)
>>> end = now + timedelta(hours=9)
>>> while now <= end:
print 'doing something at', now
now += timedelta(minutes=15)
doing something at 2013-02-09 08:00:00
doing something at 2013-02-09 08:15:00
doing something at 2013-02-09 08:30:00
doing something at 2013-02-09 08:45:00
../..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With