Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive function not returning True or False, but flows correctly and works?

Here's how the question's phrased: Write a recursive function called double letters with a single parameter astr, which is a string. The functin returns True if astr is a string containing “double letters” (two consecutive occurrences of the same letter) and False otherwise. For example, double letters("hello") returns True, where as double letters("hi there") re- turns False.

-- Not asking for anyone to do the work for me, but here's what I have. I know that it flows correctly, as if i replace return True with Print('True') and vice versa for False, it will print those. Do recursive functions not work well with boolean values or am i missing something blatantly obvious?

def double_letters(astr):
    if len(astr) >= 2:
        if astr[0] == astr[1]:
            return True
        else:
            double_letters(astr[1:])
    else:
        return(False)
like image 987
TheMuffinMan834 Avatar asked Nov 25 '25 02:11

TheMuffinMan834


1 Answers

        else:
            return double_letters(astr[1:])

Otherwise you do call recursively your function, but you discard its return value, and your function actually returns None.

like image 166
Matteo Italia Avatar answered Nov 26 '25 15:11

Matteo Italia