0

I am trying to return count in this recursive function but it always returns None. When I do print(count) instead of return count it gives me the correct result. Would anyone be able to tell me what I'm doing wrong? Thank you so much in advance :)

count = 0


def count_lowercase(s, low, high):
    global count
    if low > high:
        return count
    else:
        if s[low] == s[low].lower():
            count += 1
        count_lowercase(s, low + 1, high)
batiyar8
  • 3
  • 2

1 Answers1

4

You need to return count_lowercase(s, low + 1, high), otherwise the function simply ends after executing that statement, resulting in None being returned.

count = 0


def count_lowercase(s, low, high):
    global count
    if low > high:
        return count
    else:
        if s[low] == s[low].lower():
            count += 1
        return count_lowercase(s, low + 1, high)
krmogi
  • 2,588
  • 1
  • 10
  • 26