One more question for today: I cannot figure out why my while statement is not working (bottom of my code here). This is PS2, problem 4. I threw in some print statements (now commented out) and saw that the counter is working, but once the counter hits 4 (in the case where I’m using x=4 y=9, z=13) the process does not stop, it just keeps going, even though the while condition is no longer met. Did I format the while statement incorrectly? Thanks everyone! http://dpaste.com/580080/
I don't understand the use of the while statement in your script, among various other things. In lines 3, 4, and 5, why do you use the raw_input function, and then call to 'int'? Simply using the input() function would suffice. You declare the packages and biggestfail and finalans tuples in line 7 and 18 and 19 respectively, but nowhere else in the program do you call them. Similarly, in lines 27-30, you assign a +1 value to a,b,c, and n, but for what reason (you do not call any of those variables later in the program besides c, which is called to a for loop)? Other than that, I just didn't know exactly how the 'while' loop in your script is supposed to work. :-( I made corrections to your pasted source here http://dpaste.com/hold/580115/ It should work fine now, but I got rid of the while loop entirely.
Wow, thanks for that. Seriously could not have been more helpful, that was amazing. I understand all of your changes, using "max(biggestfail)" was a more clever solution than I had thought of. Just for my own knowledge (I know that this method is going to be much less efficient than yours, but bear with me), can you think of why my original while loop didn't work? The idea was based on the theorem in the problem set. If the smallest pack of mcnuggets has 4 pieces in it, and we find a solution for n=20,21,22,23 but not 19 then we know that 19 is the max unpurchasable. So to generalize, if x is the smallest pack of chicken, and we find solutions for c,c+1,c+2...C+(x-1), then we know that c-1 is the max unpurchasable. So the counter I put in the while loop is looking for us to hit (x-1) consecutive integers in (uniquesolns). However, something is wrong because the while loop just keeps going, even when counter>x.
I just had a quick glance so I could be wrong, and often am when I don't spend the time lol. But a for loop inside a while loop... it seems usually to complete the loop before moving on, so it would do the whole loop before it checks to see if the while condition is true. Without debugging properly, I haven't looked for any other errors.
it only checks for the loop condition when it loop end and goes back to start, a for loop in a while loop will run the entire for loop before it checks the while loop condition
don't really understand why, but the docs for the input() function says - Warning This function is not safe from user errors! and Consider using the raw_input() function for general input from users. http://docs.python.org/library/functions.html#input
it says that because input() lets the user put in raw code sure you expect a number from the user but nothing stops them from putting in something like range(100) or if they know your code well enough break your OS and run unsigned code, things like viruses or pirated programs. its a problem for apple (not the only company but the one that has the biggest problem with it)
Join our real-time social learning platform and learn together with your friends!