I am dealing with the following question:
Create a function that accepts two arguments, the number of dice rolled, and the outcome of the roll. The function returns the number of possible combinations that could produce that outcome. The number of dice can vary from 1 to 6.
And below is my code for 4 dice (x=4). But I am not able to extend this to any number of dices (x).
def dice_roll(x, y):
all_comb = []
for i in range(1, 7):
for j in range(1, 7):
for q in range(1, 7):
for r in range(1, 7):
total = i+j+q+r
all_comb.append(total)
return all_comb.count(y)
Is there a way to use recursion to deal with variable numbers of nested loops? And are there other more elegant ways apart from recursion for this question?
itertools.product. but if you only care about the sum of the possible outcomes this could be done more efficiently!xused for?