Vote count:
0
Hello these functions return the number of pigs and chickens based on the input of these two animals' heads and legs. I am really new to python. I can't understand how it works. Can you explain this elaborately?
def solve(numLegs, numHeads):
for numChick in range(0, numHeads + 1):
numPigs = numHeads - numChicks
totLegs = 4 * numPigs + 2* numChicks
if totLegs == numLegs:
return [numPigs, numChicks]
return[None, None]
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 when i run the barnYard function it asks number of heads so i put 20 heads and then put 56 legs. It prints number of pigs 8 and number chickens 12. But i really can't understand how it reaches at this stage. Specially the solve() function. Help will be appreciated.
asked 19 secs ago
Function to get the number of animals in python
Aucun commentaire:
Enregistrer un commentaire