[Tutor] class function problem
Hugo Arts
hugo.yoshi at gmail.com
Wed Sep 22 16:16:45 CEST 2010
On Wed, Sep 22, 2010 at 9:10 AM, Roelof Wobben <rwobben at hotmail.com> wrote:
>
>
> 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.
>
That's very clever. But you might argue that recursion is technically
still a loop, albeit an implicit one. There is a simpler way to do
this, without loops entirely.
Hint: repeated subtraction while your number is greater than some
constant, what you are doing, is essentially the same as doing one
division operation.
>
> 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
>
You can do "print uitkomst.seconds, uitkomst.minutes, uitkomst.hours".
Alternatively, write a suitable __str__ method for your tijd class.
Then you can just do "print uitkomst"
> Roelof
More information about the Tutor
mailing list