The question includes that you need to have a main class consisting of fields, these fields are hours minutes, and seconds. I have to use this format and I can't import anything like datetime. the question is that I need to add 2 methods first to count full minutes in our time for example let's say we have a time of 1 hour and 22 minutes the code should give 82 minutes, so convert hours into minutes and then add it all in total which I already have done. and the second method is to remove 10 minutes from the time so I have to check that the time will not go in minus so here is my code I'm just stuck at the removing 10 minutes part but can't seem to add it all together.
class Time:
def __init__(self, hour, min, sec):
self.hour = hour
self.min = min
self.sec = sec
if self.hour > 24 or self.min > 60 or self.sec > 60:
exit()
def minutes_in_full_time(self):
return self.hour * 60 self.min
def remove_ten(self):
if self.hour >= 23 and self.min >= 50 and self.sec >= 60:
print("Add 10 min" str(0) ":" str(0) ":" str(self.min - 60 - 10))
elif self.hour < 23 and self.min >= 50 and self.sec >= 60:
print("Add 10 min" str(self.hour 1) ":" str(0) ":" str(self.sec - 60 - 10))
elif self.hour < 23 and self.min < 50 and self.sec >= 60:
print("Add 10 min" str(self.hour 1) ":" str(self.min 1) ":" str(self.sec - 60 - 10))
else:
print("Add 10 min" str(self.hour) ":" str(self.min 1) ":" str(self.sec - 60 - 10))
def __del__(self):
print("Видалено")
time1 = Time(6, 34, 22)
print("amount of minutes in hours", time1.minutes_in_full_time)
time1.remove_ten()
del time1
CodePudding user response:
Here's my code:
class Time:
def __init__(self, h, m, s):
self.h = h; self.m = m; self.s = s # Compiled Code
# if self.h > 24 or self. m > 60 or self.s > 60: raise ValueError
def time_to_minute(self):
return self.h*60 self.m round(self.s/60, 2) # Round decimals to 2 digits
def increase_time(self, new_h = 0, new_m = 0, new_s = 0):
self.h = new_h; self.m = new_m; self.s = new_s;
return self.time_to_minute()
time = Time(10, 34, 22)
time_in_minutes = time.time_to_minute()
time_change = time.increase_time(new_h = -10) # Subtract 10 minutes
CodePudding user response:
class Time:
def __init__(self, hour, min, sec):
self.hour = hour
self.min = min
self.sec = sec
if self.hour > 24 or self.min > 60 or self.sec > 60:
exit()
def minutes_in_full_time(self):
return self.hour * 60 self.min
def remove_ten(self):
if self.hour >= 1 and self.min < 10:
print(" remove 10 min : " str(self.hour - 1) ":" str(self.min 60 - 10) ":" str(0))
elif self.hour < 1 and self.min < 10:
print(" remove 10 min : " str(self.hour 23) ":" str(self.min 60 - 10) ":" str(0))
else:
print(" remove 10 min : " str(self.hour) ":" str(self.min - 10) ":" str(0))
def __del__(self):
print("removed")
time1 = Time(0, 5, 5)
print("amount of minutes in hours ", time1.minutes_in_full_time())
time1.remove_ten()
del time1
time2 = Time(2, 45, 45)
print("amount of minutes in hours ", time2.minutes_in_full_time())
time2.remove_ten()
del time2