[Tutor] class function problem

Roelof Wobben rwobben at hotmail.com
Wed Sep 22 17:17:59 CEST 2010




----------------------------------------
> From: hugo.yoshi at gmail.com
> Date: Wed, 22 Sep 2010 16:16:45 +0200
> Subject: Re: [Tutor] class function problem
> To: rwobben at hotmail.com
> CC: tutor at python.org
>
> On Wed, Sep 22, 2010 at 9:10 AM, Roelof Wobben 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.

 
Sorry. I don't get it.
When I have 62 seconds that's 1 minutes and 2 seconds.
I have no clue how I can this with a division.
 
 
>
>>
>> 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 
>> 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

 
Oke, thanks for the tip. Stupid I could thought that myself.
 
 
Roelof
  		 	   		  


More information about the Tutor mailing list