List contains exactly three of the same elements?
<p>This will return Boolean True if the list has three of the same integers given. It will return a Boolean False if it does not have three of the same integers. I am having trouble writing this. Does the count feature do this? Also, is importing an empty list necessary? I have this and I get the error "'int' object has no attribute 'count'". Thank you!</p>

<pre><code>    def threeOfAKind(aList,n):
       if aList.count(n):
           return True
       else:
           return False
</code></pre>

<p>threeOfAKind([1,2,3,4,4,4],4]) should return True
I tried aList.count(n) but now I get True no matter what I call </p>

<p>This line:</p>

<pre><code>if aList.count(n):
</code></pre>

<p>is the reason why your code is not working.</p>

<p>The <code>count</code> function of <code>list</code> returns the number of occurences of <code>n</code> in the list. If your arguments are [1,2,3,4,4,4] and 4, <code>aList.count(n)</code> will return 3 because 4 occurs 3 times in the list.</p>

<p>So the line of code above is basically the same as:</p>

<pre><code>if 3:
</code></pre>

<p>Now 3 will get converted to a boolean value because that's what expected for an <code>if</code> condition. If you want to know 3 is converted to True or False, just try <code>bool(3)</code> in your interactive Python interpreter.</p>

<p>What you want is to check if <code>n</code> occures 3 times in the list, so you may need to compare the returned value of <code>aList.count(n)</code> with something? You obvisouly don't want to return True if <code>aList.count(n)</code> returns 2 or 4, do you?</p>

python list count integer