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

I am having a problem with PS2. I got part 3 (finding largest number of mcnuggets for which equation has no solution) working by using a function. However, I want to try to get it working without a function, since the class has not touched on them yet. When i tried adapting my code from using the function, to running everything inside my main while loop, I am unable to get an answer...somehow the code is getting stuck in the loop. When I first used the function, I had the print statements in the function as well. The result when the equation had a solution (e.g., n = 51)was printed outpu

OpenStudy (anonymous):

post was cut off!!! The result when the equation had a solution (e.g., n = 51) was printed output for every solution plus the statement I coded for when there was no solution for n. I compensated for this by taking the print statements out of the function and having the function return either True or False depending on whether the equation was solvable. The print statements and the adjustments to my variables were then made outside of the function. When I get rid of the function, I cannot use "return" and the variable adjustments need to be integrated into what was before a separate function. What I am finding is not only will the while loop I created does not stop once "count" equals 6 and the variable "count" will also go back to zero at some points even after reaching 6 solutions in a row, which it should not. Here is my code: http://codepad.org/Qs7RMKN5 Thanks in advance. Also, sorry if this post, doesn't make sense, I couldn't remember everything I originally included.

OpenStudy (anonymous):

i think doing it with a function is better, it makes the code easier to understand. You can use the same true/false idea without a function - you just have to use another variable to keep track of whether n can be purchased. At the Top of the while loop you initialize the variable to False then if you find (in the for loops) that it Can be bought you set it to True. After the loops have finished the else clause is executed - this would be a good place to take action on the state of your true/false variable. Here is what it might look like - i have not tested it but the general idea is there: http://dpaste.com/636934/ ... hope that doesn't spoil it for you

OpenStudy (anonymous):

First, when a for loop exits normally (not because of a break) the else loop executes. If you don't have any break statements, then the loop will always exit normally, and the "else" is unnecessary. Second, I think you're getting confused because there's not elegant way to break out of the three nested loops all at once. As bwCA suggested, if you find an answer, set a flag, and let the loops finish. It's not as efficient, but in this case I'd prefer inefficiency to inelegance. http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python Here's an interested thread about breaking out of multiple loops in python. One interesting suggestion is to raise an exception instead of breaking, and catching it after the loop you want to break out of. Much like a goto. It's bad, but probably the cleanest solution in this case. The other options I didn't completely hate were to set a flag when you break, and check the flag in the next-outer loop, and add an else: continue to the end of each loop, followed by a break. But that relies on the for/else compound statement that we didn't learn, either.

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!