Can anyone tell me whats wrong with this code. It’s supposed to list the combinations of boxes of nuggets (6,9, 20 piece) for a number of nuggets. However, some of the solutions are not being calculated.
def boxes_nuggets(nuggets):
if nuggets < 6: print "there are no sizes for less than 6 nuggets"
else:
for numSmall in range(0, nuggets/6+1):
for numMed in range(0, nuggets/9+1):
numLarge = (nuggets - 6 * numSmall - 9 * numMed)/20
if nuggets == numLarge * 20 + numMed * 9 + numSmall * 6:
print numLarge, "large boxes", numMed, "medium boxes", numSmall, "small boxes"
elif numLarge < 0: return None
I think you want to change your code to this:
So, remove the
elif numLarge < 0: return Noneand add the check for negative numLarge before printing.The problem you were having is the first time numLarge goes negative, you return, which stops the rest of the possible answers from being found.