[Tutor] class function problem
Roelof Wobben
rwobben at hotmail.com
Wed Sep 22 09:10:36 CEST 2010
HEllo,
I have this exercise :
3.Rewrite the increment function so that it doesn’t contain any loops.
The increment function looks like this :
def increment(time, seconds):
time.seconds = time.seconds + seconds
while time.seconds>= 60:
time.seconds = time.seconds - 60
time.minutes = time.minutes + 1
while time.minutes>= 60:
time.minutes = time.minutes - 60
time.hours = time.hours + 1
So I thought that recursion can do the job.
So I wrote this :
class tijd :
pass
def incrememt_seconds(time):
time.seconds = time.seconds - 60
time.minutes = time.minutes + 1
if time.seconds>= 60 :
increment_seconds(time,seconds, minutes)
return time
def increment_minutes(time):
time.minutes = time.minutes - 60
time.hours = time.hours + 1
if time.minutes>= 60 :
increment_minutes(time, minutes,hours)
return time
def increment(time, seconds):
time.seconds = time.seconds + seconds
if time.seconds>= 60 :
increment_seconds(time)
if time.minutes>= 60 :
increment_minutes(time)
return time
time = tijd()
time.hour = 20
time.minutes = 20
time.seconds = 20
seconds = 20
uitkomst = increment(time, seconds)
But how can i Check the outcome.
print uitkomst gives the object and print time(uitkomst) gives this error message :
Traceback (most recent call last):
File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 34, in <module>
print time(uitkomst)
AttributeError: tijd instance has no __call__ method
Roelof
More information about the Tutor
mailing list