How to generalize my function in iteration or recursion type?

Issue

This Content is from Stack Overflow. Question asked by 4daJKong

I have two functions,and their structure like that,

def extract(input):
    pass  #test the length of input, and the length changed over time

def checkif(s1):
    r1 = extract(s1)
    if len(r1) < 1:
        r1 = extract(s1)
        if len(r1) < 1:
            r1 = extract(s1)
            if len(r1) < 1:
                r1 = extract(s1)
                return r1
            else:
                return r1
        else:
            return r1
    else:
        return r1

you see, the function of checkif is complex and I think it can be reduced, but how to reduce this structure? Any idea is helpful!



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?