codingbat python string count
<p>Return True if the string &quot;cat&quot; and &quot;dog&quot; appear the same number of times in the given string.</p>
<p>This is the question I'm attempting. my code is:</p>
<pre><code>def cat_dog(str):
    count1 = 0
    count2 = 0
   
    if 'dog' and 'cat' not in str:
        return True
    for i in range(len(str)-1):
        if str[i:i+3] == 'cat':
            count1 += 1
        if str[i:i+3] == 'dog':
            count2 += 1
    if count1 == count2:
        return True
    else:
        return False
</code></pre>
<p>i know this is incorrect as the code does not seem to be looping through the whole string and picking up cat and dog. not sure how to rectify this.</p>