how do you add values from a list separately if one variable has two possible outcomes
<p>This assignment calls another function:</p>

<pre><code>def getPoints(n):
    n = (n-1) % 13 + 1
    if n == 1:
        return [1] + [11]
    if 2 &lt;= n &lt;= 10:
        return [n]
    if 11 &lt;= n &lt;= 13:
        return [10] 
</code></pre>

<p>So my assignment wants me to add the sum of all possible points from numbers from a list of 52 numbers. This is my code so far.</p>

<pre><code>def getPointTotal(aList):
    points = []
    for i in aList:
        points += getPoints(i)
        total = sum(points)   
    return aList, points, total
</code></pre>

<p>However the issue is that the integer 1 has two possible point values, 1 or 11. When I do the sum of the points it does everything correctly, but it adds 1 and 11 together whereas I need it to compute the sum if the integer is 1 and if the integer is 11.</p>

<p>So for example:</p>

<pre><code>&gt;&gt;&gt;getPointTotal([1,26, 12]) # 10-13 are worth 10 points( and every 13th number that equates to 10-13 using n % 13.
&gt;&gt;&gt;[21,31] # 21 if the value is 1, 31 if the value is 11.
</code></pre>

<p>Another example:</p>

<pre><code>&gt;&gt;&gt;getPointTotal([1,14]) # 14 is just 14 % 13 = 1 so, 1 and 1.
&gt;&gt;&gt;[2, 12, 22] # 1+1=2, 1+11=12, 11+11=22
</code></pre>

<p>My output is:</p>

<pre><code>&gt;&gt;&gt;getPointTotal([1,14])
&gt;&gt;&gt;[24] #It's adding all of the numbers 1+1+11+11 = 24.
</code></pre>

<p>So my question is, is how do I make it add the value 1 separately from the value 11 and vice versa. So that way when I do have 1 it would add all the values and 1 or it would add all the values and 11.</p>

<p>Rory Daulton's answer is a good one, and it efficiently gives you the different totals that are possible. I want to offer another approach which is not necessarily better than that one, just a bit different. The benefit to my approach is that you can see the sequence of scores that lead to a given total, not only the totals at the end.</p>

<pre><code>def getPointTotal(cards):
    card_scores = [getPoints(card) for card in cards] # will be a list of lists
    combos = {sorted(scores) for scores in itertools.product(*card_scores)}
    return [(scores, sum(scores)) for scores in combos]
</code></pre>

<p>The key piece of this code is the call to <code>itertools.product(*card_scores)</code>. This takes the lists you got from <code>getPoints</code> for each of the cards in the input list and gets all the combinations. So  <code>product([1, 11], [1, 11], [10])</code> will give <code>(1, 1, 10)</code>, <code>(1, 11, 10)</code>, <code>(11, 1, 10)</code>, and <code>(11, 11, 10)</code>.</p>

<p>This is probably a bit overkill for blackjack scoring where there are not going to be many variations on the scores for a given set of cards. But for a different problem (i.e. a different implementation of the <code>getPoints</code> function), it could be very interesting.</p>

python list python-3.x sum