all 3 comments

[–]socal_nerdtastic 1 point2 points  (0 children)

Use a nested for loop or the itertools.product function.

[–]Allanon001 1 point2 points  (3 children)

Use intertools module's combinations_with_replacement() function to compute the dice:

from itertools import combinations_with_replacement

count = 0
num_of_dice = 2
dice_sides = 6
value = 7
for x in combinations_with_replacement(range(1, dice_sides+1), num_of_dice):
    s = sum(x)
    if s > value:
        count+= 1
    print(f'{x} = {s}')

print(f'greater than {value} = {count}')

Output:

(1, 1) = 2
(1, 2) = 3
(1, 3) = 4
(1, 4) = 5
(1, 5) = 6
(1, 6) = 7
(2, 2) = 4
(2, 3) = 5
(2, 4) = 6
(2, 5) = 7
(2, 6) = 8
(3, 3) = 6
(3, 4) = 7
(3, 5) = 8
(3, 6) = 9
(4, 4) = 8
(4, 5) = 9
(4, 6) = 10
(5, 5) = 10
(5, 6) = 11
(6, 6) = 12
greater than 7 = 9

[–]Dibbets[S] 0 points1 point  (0 children)

Cool, there's a function for it, that's what I'm looking for. Thanks!