Detecting how many strings are present in a given variable?
<p>I'm trying to make a function that when you input a table, for example 'Fish, Carrot, Beef, Fish' it detects how many times 'Fish' was inputted, in this case 2. But when I try to do this it returns 'None' instead of 2.</p>

<pre><code>def word_count(x):
    count = 0
    for item in x:
        if item == 'Fish':
            count = count + 1
            return count
word_count(['Fish', 'Carrot', 'Beef', 'Fish'])
</code></pre>

<p>Any help would be appreciated as I'm quite new to Python, thank you.</p>

<p>It's almost correct, except:</p>

<p><code>x</code>, according to your logic should be a list of strings, so you should enclose it in <code>[]</code> when calling the function. Then you can iterate over it.</p>

<p>You should return from the function when you've got your answer, i.e. iterated over the list.</p>

<p>So the corrected version would look like</p>

<pre><code>def word_count(x):
    count = 0
    for item in x:
        if item == 'Fish':
            count = count + 1
    return count
word_count(['Fish', 'Carrot', 'Beef', 'Fish'])
</code></pre>

python