>> Personally I prefer to put the initialisation >> into an __init__() method,
> Thanks for your comments. Please, see below a new > version of that exercise. What do you think? > hour = int( raw_input('Enter the hour: ') ) > min = int( raw_input('Enter the minute: ') ) > sec = int( raw_input('Enter the sec: ') ) > > > class Time: > def __init__(self, hours = 0, minutes = 0, seconds = > 0): > self.hours = hours > self.minutes = minutes > self.seconds = seconds > > def printTime(self): # By convention, the first > parameter of a method is called self. > '''printTime: > > Prints the time.''' > print str(self.hours) + ":" + str(self.minutes) + > ":" + str(self.seconds) > > > def convertToSeconds(t): > ''' convertToSeconds: > > Converts a Time object into an integer.''' > minutes = t.hours * 60 + t.minutes > seconds = t.minutes * 60 + t.seconds > return seconds So far so good. > def makeTime(seconds): > ''' makeTime: > Converts from an integer to a Time object.''' > time = Time() > time.hours = seconds/3600 > seconds = seconds - time.hours * 3600 > time.minutes = seconds/60 > seconds = seconds - time.minutes * 60 > time.seconds = seconds > return time I'd rework this bit to: hours = seconds/3600 seconds = seconds % 3600 # use % to get remainder minutes = seconds/60 seconds = seconds % 60 #and again time = Time(hours,minutes,seconds) return time And by using divmod() you can combine two lines into one: hours,seconds = divmod(seconds,3600) minutes,seconds = divmod(seconds,60) time = Time(hours,minutes,seconds) return time > def addTime(t1, t2): > ''' addTime function: > > Calculates the sum of two times.''' > seconds = convertToSeconds(t1) + convertToSeconds(t2) > return makeTime(seconds) > currentTime = Time() > currentTime.hours = hour > currentTime.minutes = min > currentTime.seconds = sec > currentTime.printTime() I'd move the raw_input lines from the top to here so that the logical connection between them and this is clearer. And you don't use your init method here. Do this instead: currentTime = Time(hour,min,sec) currentTime.printTime() One of the principles of OOP is that you should try not to access the data members of the class directly, use methods to do all the work. [The reason for this is that we can overrride the methods in subclasses but we can't override the data so using data directly limits the effectiveness of subclassing as a mechanism, for changing the behaviour of your code without modifying it. (In Computer Science speak this is known as the Open/Closed principle - open for extension, closed for modification) ] HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor