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

@MIT 6.00 Lesson 4, def solve2(numLegs, numHeads) doesn't work on my Python 2.7.2. Any idea? @MIT 6.00 Lesson 4, def solve2(numLegs, numHeads) doesn't work on my Python 2.7.2. Any idea? @MIT 6.00 Intro Co…

OpenStudy (anonymous):

Any error messages? What is the output of it? Can you paste your code in codepad, so I can check if you indented it correctly or other minor errors like that? I copied the code verbatim and it worked fine for me.

OpenStudy (anonymous):

No error message. Any input will result in "There is no solution." -------------- def solve2(numLegs, numHeads): solutionFound = False for numSpiders in range(0, numHeads + 1): for numChicks in range(0, numHeads -numSpiders + 1): numPigs = numHeads - numChicks - numSpiders totLegs = 4*numPigs + 2*numChicks + 8*numSpiders if totLegs == numLegs: print 'Number of pigs: ', str(numPigs) print 'Number of chickens: ', str(numChicks) print 'Number of spiders:', str(numSpiders) solutionFound = True if not solutionFound: print 'There is no solution.'

OpenStudy (anonymous):

My own code of doing same thing, without parameters, works just fine. ---------------------------------- def barn(): """ This lists all possible solutions """ numHeads = int(raw_input('Enter number of heads: ')) numLegs = int(raw_input('Enter number of legs: ')) count = 0 for numSpiders in range(0, numHeads + 1): for numChicks in range(0, numHeads -numSpiders + 1): numPigs = numHeads - numChicks - numSpiders totLegs = 4*numPigs + 2*numChicks + 8*numSpiders if totLegs == numLegs: count = count + 1 print 'Number', str(count), 'of pigs:', numPigs print 'Number', str(count), 'of chickens:', numChicks print 'Number', str(count), 'of spiders:', numSpiders if count == 0: print 'There is no solution'

OpenStudy (anonymous):

Anybody, any clues?

OpenStudy (anonymous):

I just copy and pasted your code that wasn't working and it did work for me. Are you trying correct inputs? That's awkward.

OpenStudy (anonymous):

You're right, I was making a really stupid mistake, I was always putting in numbers like (10,20)/(numHeads, numLegs) instead of (20,10)/(numLegs, numHeads). Many thanks, bmp!

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!