Count the number of times each element appears in a list. Then making a conditional statement from that
<p>write a function that takes, as an argument, a list called aList. It returns a Boolean True if the list contains each of the integers between 1 and 6 exactly once, and False otherwise.</p>

<p>This is homework and I thought I had it right, but it is now telling me that it isn't right. Here is my code.</p>

<pre><code>def isItAStraight(aList):
    count = 0
    for i in set(aList):
        count += 1
        return aList.count(i) == 1
</code></pre>

<p>for some reason even if a number appears more than once it still gives true and I can't figure out why it won't give me false unless the first or last number are changed.</p>

<p>Each number has to occur only one time otherwise it is false.</p>

<p>So like take [1,2,3,4,5,6]
would be true.
But [1,2,2,3,4,5]
would be false.</p>

<p>Also, I can't import things like Counter or collections (though it would be so much easier to do it isn't apart of the assignment.)</p>

<p>The list is randomly generated from 1 to 6.</p>

<p>You need to be careful about what's <em>inside</em> the list.  What you've written is a basically the same as the pseudo-code below:</p>

<pre><code>let count = 0
for every unique element in aList:
   Add 1 to count
   if count is now 1, return true.
</code></pre>

<p>This will <em>always</em> return true if there is at least one element in <code>aList</code>, since you're adding 1 to count and then returning immediately.</p>

<p>A couple approaches to consider:</p>

<ol>
<li>Create a 6 element list of all zeros called <code>flags</code>.  Iterate over <code>aList</code> and set the corresponding element in <code>flags</code> to <code>1</code>.  If <code>flags</code> is all ones, then you return true.</li>
<li>Sort the list, then check if the first six numbers are <code>1, 2, 3, 4, 5, 6</code>.</li>
</ol>

python list python-3.x count