How to count total minutes and seconds in Python

I need to count all the minutes from the third list items from a list. The answer should be 10.00 (because it is in a matter of minutes). I am stuck in error i is not defined. This is the code:

def main():
    a=[("a","b", 3.10),
       ("c","d", 3.20),
       ("e","f", 3.30)]
    for i[2] in a:
        c=(i[2]//1)*60
        d=(i[2]-(i[2]//1))*100
        e=c+d
        f=f+e
    g=f%60
    h=f//60
    i=g/100
    f=i+h
    print(f) 
+4
source share
1 answer

You need to apply 2 fixes to make them work:

  • use iinstead i[2]in the loop linefor
  • initialize fbefore loop:

Corresponding part of the code with the applied corrections:

f=0
for i in a:
    c=(i[2]//1)*60
    d=(i[2]-(i[2]//1))*100
    e=c+d
    f=f+e 

To sum the time in your case, you can use timedeltas:

from datetime import timedelta
import math

a=[("a","b", 3.10),
   ("c","d", 3.20),
   ("e","f", 3.30)]

s = timedelta()
for item in a:
    seconds, minutes = math.modf(item[2])
    s += timedelta(minutes=minutes, seconds=seconds*100)

print s.seconds / 60  # prints 10
+5
source

Source: https://habr.com/ru/post/1536292/


All Articles