Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

for the barnyard problem in lecture four, what does this line of code mean: pigs, chickens = solve(legs, heads)? i get that its passing two parameters to the function solve but wouldn't that mean that pigs and chickens are equal to the same value?

OpenStudy (anonymous):

heres the whole code: def solve(numLegs, numHeads): for numChicks 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 print 'Number of chickens:', chickens

OpenStudy (goodbye):

ok , im a total newb. But i THINK i can help you with the meaning of that line. pigs, chickens= solve(legs, heads) right, so "legs" and "heads" are the two parameters being passed to the function "solve". The function solve then runs, using those two paramters (legs & heads). When the funtion solve is finished, it RETURNS two parameters: "numPigs" and "numChicks". so the line: "pigs, chickens= solve(legs, heads)" is actually sayin make "pigs" equal the first value that is returned from the "solve(legs, heads), i.e. make pigs=numPigs. the second value (after the comma), "chickens" is being made equal to the second value that the solve function "returns", which is: numChicks I hope I have answered the question you were asking, I may have just answered the question I THINK you were asking. And of course, my explanation is really bad, someone else will come along and clear it up for you in a while anyway. If it returns (None,None) instead of (numPigs,numChicks) then that is because, it could not find a solution i.e. There is no solution. I'ts sad....I have done that exercise, but looking back i can barely (read: cannot) understand the math behind how it works. i see you are replying, i wanna post my reply before u tell me u solved it yourself

OpenStudy (goodbye):

oh, someone else was replying, not you...oh well

OpenStudy (anonymous):

Okay well obviously I was beaten in the race to give you an answer but goodbye is correct. To put it simply, 'pigs' will take the value numPigs (or None) 'chickens' will take the value numChicks (or None)

OpenStudy (anonymous):

yeah thats what i was thinking but wanted to make sure, thanks

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!