Advertisement
teslariu

desafio conv horaria

Jan 16th, 2023
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Crear una función que reciba un número como argumento, ese número
  5. representará los segundos que se quieren convertir a horas, minutos y
  6. segundos.
  7. Por ejemplo, conversion(3600) retornaría el texto
  8. “01:00:00” , en cambio si recibe 1000, devolverá
  9. “00:16:40”
  10. No se puede usar ninguna función built-in, ni
  11. tampoco ningún módulo que haga la conversión.
  12. """
  13.  
  14. def conversion(seg):
  15.     minutos = 0
  16.     horas = seg // 3600
  17.     segundos = seg % 3600
  18.     if segundos >= 60:
  19.         minutos = segundos // 60
  20.         segundos = segundos % 60
  21.     if horas < 10:
  22.         horas = f"0{horas}"
  23.     if minutos < 10:
  24.         minutos = f"0{minutos}"
  25.     if segundos < 10:
  26.         segundos = f"0{segundos}"
  27.            
  28.     return f"{horas}:{minutos}:{segundos}"
  29.        
  30.        
  31. def ingresar():
  32.     while True:
  33.         try:
  34.             tiempo = int(input("Ingrese el tiempo en segundos: "))
  35.         except ValueError:
  36.             print("Debe ingresar un nro entero de segundos")
  37.         else:
  38.             if tiempo >= 0:
  39.                 return tiempo
  40.             else:
  41.                 print("El tiempo debe ser no negativo")
  42.            
  43. t = ingresar()
  44. print(conversion(t))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement