[Tutor] Is this correct?
Alan Gauld
alan.gauld at freenet.co.uk
Sun Apr 16 01:12:16 CEST 2006
Hi,
> The goal is to write a function printTime that takes a
> Time object as an argument and prints it in the form
> hours:minutes:seconds.
>
> So, I wrote the script below:
>
> class Time:
> pass
>
> hour = int( raw_input('Enter the hour: ') )
> min = int( raw_input('Enter the minute: ') )
> sec = int( raw_input('Enter the sec: ') )
>
> time = Time()
> time.hours = hour
> time.minutes = min
> time.seconds = sec
Personally I prefer to put the initialisation intomnan __init__()
method, but otherwise it looks ok to me. Also I'd probably
not use a class here but a simple tuple or similar, but your
exercise seems to want an object and implies a class should be used.
> def printTime(time):
> print 'The time is %dh:%dmin:%dsec' % (hour, min,
> sec)
>
> printTime(time)
HTH,
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list