Basically:
def solve(numLegs, numHeads):
for numChick in range(0, numHeads + 1): #for every number in the range 0 - the number of heads + 1, numChick = that number
numPigs = numHeads - numChicks #the number of Pigs equals the number of heads entered minus the current number
totLegs = 4 * numPigs + 2* numChicks #the amount of legs is 4(amount of legs) * number of heads + 2(chicken legs) * the current number
if totLegs == numLegs: if the pigs legs + the chicken legs = the total number of legs,
return [numPigs, numChicks] #return the number of pigs and chickens
return[None, None] #else, return none, triggering "no solution"
def barnYard():
heads = int(raw_input('Enter number of heads:')) #
legs = int(raw_input('Enter number of legs:'))
pigs, chickens = solve(legs, heads)
if pigs = None:
print 'there is no solution'
else:
print 'number of pigs:' , pigs
pirnt 'number of chickes:', chickens
So:
Basically, it will run the function over and over until the amount of legs calculated equals the total amount of legs entered. If it never equals up, it just returns [none, none]. (return [numPigs, numChicks] breaks the for loop)
Edit:
I tried taking out the +1 in line 2 and it still worked just fine.