Я пытаюсь заставить это работать, похоже, проблема в том, когда я пытаюсь добавить час и десятичное время (например, .75, равное 45 минутам). Я думаю, что мне нужно что-то добавить к init (), однако я не уверен, что это решит. Поплавок?
class Time():
def __init__(self,a=0.0,b=0.0):
self.hour = a
self.minute = b
self.hour_dec = a-int(a)
self.total_hour = self.hour - self.hour_dec
self.extraMin = self.hour_dec * 60
self.total_min = self.minute + self.extraMin
if self.total_min > 60:
self.total_min= self.total_min - 60
self.total_hour += 1
def __str__(self):
return str(self.total_hour)+" hours, "+str(self.total_min)+" minutes "
def __add__(self, other): #Brad suggested the if statement to get this part to work
if isinstance(other,int):
newHour = self.total_hour + other
newMinute = self.total_min
return Time(newHour,newMinute)
else:
newHour = self.total_hour + other.total_hour
newMinute = self.total_min + other.total_min
return Time(newHour,newMinute)
def main():
time0= Time()
print(time0)
time1 = Time(6)
print(time1)
time2 = Time(3,30)
print(time2)
time3 = Time(30,75)
print(time3)
time4 = Time(3.5)
print(time4)
print(time3 + time4)
print(time3)
print(time1 + 10)
print(.75 + time2)
main()