A community for students.
Here's the question you clicked on:
 0 viewing
paul99santos
 2 years ago
I watching this lecture video and I get a wrong result,( skip video to 29:53). link:
http://ocw.mit.edu/courses/electricalengineeringandcomputerscience/600introductiontocomputerscienceandprogrammingfall2008/videolectures/lecture4

Does anyone have the source code or can make one the same? I made my own code but it displays the wrong result. can you comment on my source code what I did wrong?
my source 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 Chickens: ', chickens
>>> barnYard()
Enter number of Heads: 20
Enter number of Legs: 56
There is no solution
It should be
Enter number of Heads: 20
Enter number of Legs: 56
Number of pigs: 8
Number Chickens: 12
paul99santos
 2 years ago
I watching this lecture video and I get a wrong result,( skip video to 29:53). link: http://ocw.mit.edu/courses/electricalengineeringandcomputerscience/600introductiontocomputerscienceandprogrammingfall2008/videolectures/lecture4  Does anyone have the source code or can make one the same? I made my own code but it displays the wrong result. can you comment on my source code what I did wrong? my source 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 Chickens: ', chickens >>> barnYard() Enter number of Heads: 20 Enter number of Legs: 56 There is no solution It should be Enter number of Heads: 20 Enter number of Legs: 56 Number of pigs: 8 Number Chickens: 12

This Question is Closed
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.