Object oriented python, I want to create a static method to convert hours, minutes and seconds into seconds.
I create a class called Duration:
class Duration:
def __init__(self, hours, minutes, seconds):
self.hours = hours
self.minutes = minutes
self.seconds = seconds
I then create a variable named duration1 where I give it the numbers
duration1 = Duration(29, 7, 10)
I have a method called info which checks if any of the numbers are less than 10, and if so add a "0" in front of it, and then i later revert the values into ints.
def info(self):
if self.hours < 10:
self.hours = "0" + str(self.hours)
elif self.minutes < 10:
self.minutes = "0" + str(self.minutes)
elif self.seconds < 10:
self.seconds = "0" + str(self.seconds)
info_string = str(self.hours) + "-" + str(self.minutes) + "-" + str(self.seconds)
self.hours = int(self.hours)
self.minutes = int(self.minutes)
self.seconds = int(self.seconds)
return info_string
And now I want to create a static_method to convert these values into seconds, and return the value so i can call it with duration1 (atleast I think thats how i should do it..?
@staticmethod
def static_method(self):
hour_to_seconds = self.hours * 3600
minutes_to_seconds = self.minutes * 60
converted_to_seconds = hours_to_seconds + minutes_to_seconds \
+ self.seconds
return converted_to_seconds
duration1.static_method(duration1.info())
I guess my question is how would I use a static method with this code to change the numbers? I should also tell you this is a school assignment so I have to use a static method to solve the problem. Any help is greatly appreciated!
Error message says this:
hour_to_seconds = self.hours * 3600
AttributeError: 'str' object has no attribute 'hours'
selfparameter.