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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With