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

Hi guys,why after I run this code, the error message shows "expect the intended error" . the numpigs in the third line is marked def solve(numlegs, numheads): for numchicks in range (0, numheads): numpigs = numheads - numchicks totlegs = 4*numpigs + 2*numchicks if totlegs == numlegs: return (numpigs, numchicks) return (None,None) def barnyard(): heads = int(raw_input( 'head:')) legs = int(raw_input('legs')) pigs, chickens = solve(heads, legs) if pigs = None: print('there is no') else: print (pigs) print(chickens)

OpenStudy (anonymous):

Is this python? I believe you need to indent the code within your for-loop too: def solve(numlegs, numheads): for numchicks in range (0, numheads): numpigs = numheads - numchicks totlegs = 4*numpigs + 2*numchicks if totlegs == numlegs: return (numpigs, numchicks) return (None,None)

OpenStudy (anonymous):

Ehm I believe I misread your for-loop, but you have an indentation problem there :) Whatever is in the loop should be indented.

OpenStudy (anonymous):

these two lines need another level of indentation: numpigs = numheads - numchicks totlegs = 4*numpigs + 2*numchicks you also have a nasty bug within the last if statement (can you find it?)

OpenStudy (harsimran_hs4):

@crz1990 use code pasting sites like pastebin.com etc it`s easier to read and help

OpenStudy (rsmith6559):

else: print (pigs) print(chickens) This line has two statements after the else:. If this is Python v2.x, print doesn't have parentheses around the argument, that's Python v3.x. The code that was posted does have indentation errors.

OpenStudy (e.mccormick):

If you put \(\text{```}\) above and below the code, it also formats it properly... though I only suggest this for small bits of code: ``` def solve(numlegs, numheads): for numchicks in range (0, numheads): numpigs = numheads - numchicks totlegs = 4*numpigs + 2*numchicks if totlegs == numlegs: return (numpigs, numchicks) return (None,None) ``` It turns on the "preformatted block" mode in the message.

OpenStudy (harsimran_hs4):

@e.mccormick shouldn`t return(None, None) be outside the for loop ?

OpenStudy (e.mccormick):

Probably... I was just showing the ``` block quote system, which means I had to copy and paste code here, which made it collapse, so I had to reformat it. The point was that if they use the block quote, those issues with copy and paste do not happen.

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!