【发布时间】:2021-12-10 12:58:29
【问题描述】:
我正在尝试递归查找字符串中小写字母的数量,该字符串具有确定需要考虑字符串的哪一部分的低和高索引:def count_lowercase(s, low, high)。由于if s[count] == s[count].lower() and s[count] != ' ',一旦遇到大写字母或空格,我的代码就会停止遍历字符串,但我无法弄清楚如何让它继续。非常感谢任何帮助!
我的代码
def count_lowercase(s, low, high):
def countfunc(s2=s[low: high+1], count=0):
if count > high:
return count
else:
if s[count] == s[count].lower() and s[count] != ' ':
return countfunc(s2, count+1)
return count
return countfunc()
【问题讨论】:
-
需要递归吗?