I want my program to find the combinations of x integers with values in a range that summed equal a given number.
For example: I want to find the combinations of 3 integers that can have a value between 0 and 2 and that summed equal 5. I can do this by coding:
possibilities = []
total = 5
valueRange = 3
for num1 in xrange(valueRange):
for num2 in xrange(valueRange):
for num3 in xrange(valueRange):
if num1 + num2 + num3 == total:
possibilities.append([num1, num2, num3])
I can change the value of the total sum and the range value by changing the variables I created, but how can I specify the amount of nested loops using a variable? Thank you