1

I am writing a "clean" method for a "Time" class. The clean method would take a Time object and would make sure that the number of seconds and minutes is between 0 and 59. I am running into an error with recursion. Here is what I have so far:

def clean(self):
    '''Adjust Time object so that the number of seconds and minutes
       is between 0 and 59'''

    if self.S < 60:
        self.S
    else:
        self.M = int(self.S/60)+self.M
        self.S = self.S%60


    if self.M < 60:
        self.M
    else:
        self.H = int(self.M/60)+ self.H
        self.M = self.M%60


    if isinstance(self.H,float) == True:
        self.S = self.H * 3600
        self.clean()
    else:
        self.H

        return self.__str__()
1
  • I believe if self.S < 60: self.S is dead code. Same for the other if statement. Commented Apr 8, 2015 at 23:22

2 Answers 2

2

In your code:

if isinstance(self.H,float) == True:
    self.S = self.H * 3600
    self.clean()
else:

You don't change self.H if this is True, so it will always be true. I'm guessing you want to set self.H = 0 or something like that (since you converted it all to seconds).

Also, in your if statements, having 'self.S' doesn't actually do anything, so you should write:

if self.S < 60:
    self.S
else:
    self.M = int(self.S/60)+self.M
    self.S = self.S%60

as

if self.S >= 60:
    self.M = int(self.S/60)+self.M
    self.S = self.S%60

Also, in your recursive check, you destroy your previous value of self.S, did you mean to add that?

EDIT: You don't specify in your question, but from your code it looks like you're assuming the time values (S, M, H) are valid and you're just shifting the quantities around to get the same total time with S < 60 and M < 60. If this is the case, then you can easily eliminate the recursion by converting everything to seconds at the beginning and then go through your S and M steps:

self.S = self.H * 3600 + self.M * 60 + self.S

self.M = int(self.S / 60)
self.H = int(self.M / 60) # or = int(self.S / 3600)

self.M = self.M % 60
self.S = self.S % 60
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like the only place where recursion happens is here:

if isinstance(self.H,float) == True:
    self.S = self.H * 3600
    self.clean()

If you make self.H not a float then it won't repeat:

if isinstance(self.H,float) == True:
    self.S = self.H * 3600
    self.H = int(self.H)    # add this line
    self.clean()

Better yet, adjust the values at the top so you don't need recursion:

# move this to the first lines of your method
if isinstance(self.H,float) == True:
    self.S = self.H * 3600

1 Comment

I would also eliminate the recursion in this case, but without more info on what the data is, you may need a little more than just converting hours to seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.