I have the following code which must increment the value of the attribute by 1, taking into account the following conditions:
If the value of minutes is equal to 59 before the increment, then if the value of hours is less than 23, the method will increment by 1 hours and zero out the value of minutes, but if instead the value of hours is equal to 23, being the minutes equal to 59, the method will set both attributes to zero It fails me in one of those "tests"
class Time:
"""Clase que muestra el tiempo"""
def __init__(self, h, m):
"""Time objects are initialized with hours and minutes"""
self.horas = h
self.minutos = m
def tiempo_incrementacion(self):
if self.minutos == 59 or self.horas == 23:
if self.horas < 23:
self.horas += 1
self.minutos = 0
elif self.minutos == 59:
self.horas = 0
self.minutos = 0
t = Time(12, 30)
print("Horas = {} y minutos = {}".format(t.horas, t.minutos)) # Prints "Horas = 12 y minutos = 30"
t.increase()
print("Horas = {} y minutos = {}".format(t.horas, t.minutos)) # Prints "Horas = 12 y minutos = 31"
t = Time(12, 59)
print("Horas = {} y minutos = {}".format(t.horas, t.minutos)) # Prints "Horas = 12 y minutos = 59"
t.increase()
print("Horas = {} y minutos = {}".format(t.horas, t.minutos)) # Prints "Horas = 13 y minutos = 0"
t = Time(23, 59)
print("Horas = {} y minutos = {}".format(t.horas, t.minutos)) # Prints "Horas = 23 y minutos = 59"
t.increase()
print("Horas = {} y minutos = {}".format(t.horas, t.minutos)) # Prints "Horas = 0 y minutos = 0"
Look
Only change it if the minutes are 59 then minutes = 0 and we are going to check if the hour is 23 and if it is 23 it is changed to 0 otherwise 1 is added and if the minutes are not 59 it is added 1
Perhaps the recommendation for this type of case would be to use the operator
%
in order to avoid nesting of conditionalsif... else
.This operator
%
is useful because it allows you to define cycles like the clock by dividing the number that increases or decreases (numerator) and the limit of the cycle (denominator), thus, for example, in the case of hoursTherefore, a possible implementation of the "Cronometro" class would be as follows (this class also includes the report - lookCronometro - so as not to be repeating printing code)