Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python comes with a <a href="http://docs.python.org/2/library/" rel="nofollow">great standard library</a> (as you might already discovered since you are utilizing <em>itertools</em>), in which you'll also find a <a href="http://docs.python.org/2/library/random.html" rel="nofollow">random</a> module.</p> <p>You could use <a href="http://docs.python.org/2/library/random.html#random.randint" rel="nofollow">random.randint</a> to simulate dice rolls. There are multiple ways to solve this. The first code example is somewhat limited, the second is more general.</p> <pre><code>import random # '_' (underscore) is used for values that are generated, but that you do not # care about - here we only want to repeat seven times and do not care about # the actual loop count for _ in range(7): # generate three random numbers between [1 and 6] # and store the values in a, b, c respectively (tuple unpacking) a, b, c = (random.randint(1, 6) for _ in range(3)) # if one of the conditions holds, break out of the loop early if a == b or a == c or b == c or a == b == c: break </code></pre> <p>As @Paulo points out, you can use another <strong>more concise</strong> approach to check, whether the <code>n</code> elements of a list (or tuple) are all different, that is you put all elements in a <a href="http://docs.python.org/2/library/stdtypes.html#set" rel="nofollow">set</a>:</p> <pre><code>for _ in range(7): # instead of three separate variables, we use a list here # the nice thing is, that you can freely vary the number of # 'parallel' dice rolls this way dice = [random.randint(1, 6) for _ in range(3)] # this is more general and will break as soon as there are # duplicate (die) values in the list (meaning, break, if not all elements # are different) if len(dice) &gt; len(set(dice)): break </code></pre> <p>Answering your updated question, just use <a href="http://docs.python.org/2/library/functions.html#sum" rel="nofollow"><code>sum</code></a>:</p> <pre><code> total = sum(dice) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload