我试图返回与功能参数具有相同起始字符和结束字符的子字符串计数。我相信我的逻辑很好(我必须使用递归进行作业),但是我无法获得返回到终端的结果。
我已经使用print语句来跟踪某些值,但是无法获取返回结果的函数。
def countSubstring(str_, substr_):
# Base Case - check string length
if len(str_) == 0:
return 0
else:
# Split String to List
strList = str_.split()
# Get substring, shorten list
substr = strList.pop()
# Convert strList to string
newstr_ = ' '.join(strList)
# check if starts, ends with character
if (substr.startswith(substr_[0]) and substr.endswith(substr_[-1])):
# Recursive call with shortened string and add to count
return 1 + countSubstring(newstr_,substr_)
#return 1 + countSubstring(newstr_,substr_)
# Recursive call with shortened string
#return countSubstring(newstr_,substr_)
return countSubstring(newstr_,substr_)
countSubstring("bob", "bob")
预期结果= 1 实际结果=终端上什么都没有显示