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>With a <code>return</code> inside the loop, you are only checking one value. You need to check each value. Also, instead of looping through the items of the list, you should loop through the items you're actually looking for. It would also help to make sure there are the correct number of items in the list.</p>

<pre><code>def isItAStraight(aList):
    if len(aList) != 6:
        return False
    for i in range(1, 7):
        if aList.count(i) != 1:
            return False
    return True
</code></pre>

<p>But the easiest way to do this is to simply sort the list and check if it's what you're looking for:</p>

<pre><code>def isItAStraight(aList):
    return sorted(aList) == list(range(1, 7))
</code></pre>

python list python-3.x count