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

Can someone help explain the logic of a short piece of code to me? http://dpaste.com/579844/ I need help understanding how python is doing "guess and check" here. I guess I don't exactly understand the sequence of events it follows. So we start with 0,0,0 (a,b,c), and it looks like it now tries (0,0,1), or does it try (1,1,1)? Also, let's say N=50 here, so that the for statement means C can only be 0,1,2,3... does this work that once c=3, if we add 1 to it it will go back to 0? Thanks for any help!

OpenStudy (anonymous):

It should go (0,0,0), (0,0,0), (0,0,0), (0,0,1), (0,0,2) .... (0,1,0), (0,1,1) ..... Put some print statements in you code like this and you can see what's going on. ( http://dpaste.com/579855 Not sure if I understand your 2nd question, but If n=50 range(0,50/20+1)=[0, 1, 2], so that's what it will iterate over.

OpenStudy (anonymous):

For n = 50: It will set a value for a, starting with 0. Then it will do lots of iterations with a = 0. The first batch of those iterations with a = 0 will have b = 0 and do four iterations with those settings, one with c = 0 then c = 1 then c =2 and finally, c = 3. Then it does the next batch of b iterations. So a is still 0. b is now 1. And c passes through 0, 1, 2, and 3 again. It repeats that process with a = 0 until it completes the last batch of b = 6 (50 div 9 = 5, add 1 and you get 6). Then a gets one added to it, and it repeats the whole thing over again. Like mk95 says, you will get iterations like a=0, b=0, c=0 a=0, b=0, c=1 a=0, b=0, c=2 a=0, b=0, c=3 a=0, b=1, c=0 a=0, b=1, c=1 and so on to a=0, b=6, c=3 a=1, b=0, c=0 a=1, b=0, c=1 a=1, b=0, c=2 As you can see, both c and b go back to 0 once their loop completes.

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!