0

My task is to get a list of all possible variations of time between 02:19:30 and 11:09:29, so it be: [02:19:30, 02:19:31, ... , 11:09:29].

I wrote this piece of code to iterate through seconds('print' will be replaced by list appending):

start_time = datetime.time(2, 19, 30)
end_time = datetime.time(11, 9, 29)
delta = datetime.timedelta(seconds=1)

while start_time != end_time:
    print(str(start_time))
    start_time += delta

But I keep getting this error:

Traceback (most recent call last): File "F:\PythonProjects\Python-lab5\main.py", line 16, in start_time += delta TypeError: unsupported operand type(s) for +=: 'datetime.time' and 'datetime.timedelta'

What is wrong?

Nazar
  • 57
  • 7

1 Answers1

1

times are "abstract" as it is - you'll need to work with full datetimes if you want to do arithmetic with them.

start_time = datetime.datetime(2021, 1, 1, 2, 19, 30)
end_time = datetime.datetime(2021, 1, 1, 11, 9, 29)
delta = datetime.timedelta(seconds=1)

while start_time != end_time:
    print(str(start_time.time()))
    start_time += delta
AKX
  • 152,115
  • 15
  • 115
  • 172