所以这是我的代码
def count_occurrences(sub, s):
if len(s) == 0:
return 0
else:
if str(sub) in str(s) and str(sub) == str(s):
return 1+count_occurrences(sub, s[1:])
else:
return count_occurrences(sub, s[1:])
print(count_occurrences('ill', 'Bill will still get ill'))
我相信if str(sub) in str(s) and str(sub) == str(s):
当我运行调试器 UI 时,这句话让我大吃一惊。如果我只是把if str(sub) in str(s)
它给了我一个数字,但它不是我想要的数字,它是 4。