-1

I want to get the total of times by doing this inner for loop...However it seems like I am doing something wrong here, because I am getting the error AttributeError: 'datetime.datetime' object has no attribute 'timedelta'...

from datetime import datetime, timedelta
N = int(input())
lista = []
for n in range(N):
    name = input()
    times = [datetime.strptime(m, '%S.%f') for m in input().split()]
    initialTime = datetime(1900,1,1,0,0,0)
    for m in times:
        initialTime += initialTime.timedelta(m)
    lista.append(initialTime)
print(lista)

I am also giving you some sample data:

5
Guilherme
20.252 20.654 20.602
Edison
24.000 24.024 23.982
Caetano
20.380 25.816 21.739
Geraldo
20.310 25.725 21.664
Luis
20.289 25.699 21.643

Which is intended to display the following result

["1:01.508", "1:12.006", "1:07.935", "1:07.699", "1:07.631"]
lucasbbs
  • 349
  • 4
  • 14
  • It's not clear what the line `initialTime.timedelta(m)` is supposed to be doing, as there's no assigning happening. `timedelta` is a class, it's not a method of a `datetime` object: https://docs.python.org/3/library/datetime.html#timedelta-objects Maybe your confusion is that the object is located at `datetime.timedelta`. That doesn't mean it's a property of a `datetime.datetime` object, that means it's a class in the `datetime` library. If you want to continuously increment `initialTime`, then you'd have to do `initialTime += (m - initialTime)`, assuming `m` is sooner than `initialTime`. – Random Davis Sep 13 '21 at 21:01
  • Hello, @RandomDavis, I have done some editting hopefully now it will make some sense for you – lucasbbs Sep 13 '21 at 21:03
  • I edited my comment to point out the errors that I see. A `timedelta` object can either be instantiated outright, or created by subtracting two dates. – Random Davis Sep 13 '21 at 21:04
  • Ok, @RandomDavis, I got what you mean... Is there any workaround for what I am trying to do? – lucasbbs Sep 13 '21 at 21:07
  • Yes there is. Please read my comment, I edited it again to suggest a workaround. – Random Davis Sep 13 '21 at 21:08
  • However what is the point of adding the difference between `m` and `initialTime` to `initialTime`? That would just create a date object identical to `m`, wouldn't it? It seems like `lista` and `times` would contain identical items. Unless my thinking is just way wrong. – Random Davis Sep 13 '21 at 21:10
  • I just want to sum up the seconds and miliseconds bellow each name, which is being read – lucasbbs Sep 13 '21 at 21:15
  • Okay, hopefully now that you have been given the resources to know how to use `timedelta` correctly, you should be able to figure the rest out. If you can't then let us know. – Random Davis Sep 13 '21 at 21:19

1 Answers1

0

It looks to me like you are trying to add up three "lap" times to get a total time for each entry. For that, you don't need initialTime at all. You can convert all the time entries to timedelta objects and use sum() to add them up.

Inside your outer loop:

times = input().split()
for i in range(len(times)):
    t = datetime.strptime(times[i], '%S.%f')
    times[i] = timedelta(minutes=t.minute, seconds=t.second, microseconds=t.microsecond)
lista.append(sum(times, timedelta()))

The output will be a list of timedelta objects. They can be easily converted to strings with str() if needed.

Noah S.
  • 46
  • 5