def solve(numLegs, numHeads):
for numSpiders in range(0, numHeads + 1):
for numChicks in range(0, numHeads - numSpiders + 1):
numPigs = numHeads - numChicks - numSpiders
totLegs = 4*numPigs + 2*numChicks + 6*numSpiders
if totLegs == numLegs:
return [numPigs, numChicks, numSpiders]
return [None, None, None]
def barnYard(heads, legs):
pigs, chickens, spiders = solve(legs, heads)
if pigs == None:
print "There is no solution."
else:
print 'Number of pigs: ', pigs
print 'Number of Chickens: ', chickens
print 'Number of Spider: ', spiders
barnYard(20,56) # 8 pigs - 12 chickens
barnYard(21,62) # 10 pig - 11 chickens
20 heads and 56 legs returns 8 pigs and 12 chickens, so I made it 21 and 62 to add a spider, but it still returns pigs and chickens, whats wrong in the code?
Thanks!