Convert time to number seconds without decimal part

Asked

Viewed 696 times

-1

I’m having trouble presenting the result of the transformation of a specific time, in the form HH:MM:SS, in seconds. The result is correct, but with a .0 in the end, which makes my response invalid on a website.

My code:

horario = input()
hora = float(horario[0:2])* int(3600)
minuto = float(horario[3:5])* int(60)
segundo = float(horario[6:8])
C = float(hora + minuto + segundo)
print(int(C))

# Saída: 18711.0
  • 4

    There’s no way out with the .0 in the end is using the int. Are you sure this is the code you executed? If so, please elaborate a [mcve] on https://repl.it/languages/python3 demonstrating the problem.

1 answer

0

Reading your code and testing it, the answer is an integer value even because there is no way to get float if you are converting at the end, in any case I set an example just to compare, I used the split only to stay on a vector every hour/min/sec to not need to do the string cuts, it separates in every occurrence of the character ':'

hora = input()
vetor = hora.split(":")
soma = 0
soma += int(vetor[0])*3600
soma += int(vetor[1])*60
soma += int(vetor[2])
print(soma)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.