I'm working on Assignment 2, the third part. I'm not understanding why my code keeps running without giving any answer, and how I could go about debugging it. http://dpaste.com/553827/
The two methods I use best for debugging code are as follows: 1)print statements. Whereever you think will help you to understand what is happening in each part of the program... You can put in as many as you like, you just remove them at the end. 2)(as shown in video) get a paper, make a column for each variable, put the opening value in, work through the program changing values as you go. PS I am only up to lecture 6 myself.
you have an infinite loop going on
it might not even be an infinite loop it just might be taking forever because you have 3 nested for loops that run around inside a while statement multiple times until 6 in a row are reached but i doubt it would take that long. its worth checking though... i solved this one a little bit differently so im not exactly sure how to fix yours but heres what i did http://dpaste.com/553928/
first, it looks like you have tried to optimize your code before you have it working - not a good idea. early optimization to the dark side leads. check this out: http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Overview:_Optimize_what_needs_optimizing keep it simple at first; restructure your code to have three inner loops - one each for 6, 9, and 20 nugget packs. just let it run through all the combinations to see if a specific number of nuggets can be bought - you do need to figure out a way to keep track of this. you only want to change your consecutivenuggets variable Outside of these loops - in other words once you have tested all combinations of 6, 9 and 20 packs and found whether a specific number of nuggets can(not) be bought, then change consecutivenuggets. i can't remember how long it takes for these to run - you can put print statements in the outer loop so you can tell it is running. after you get this working, you can figure out ways to break out of the loops when they are doing xtra work.
Join our real-time social learning platform and learn together with your friends!