PS2, Q3: I can't figure out why I get stuck in an infinite loop when I run this code--or more generally why it isn't functioning smoothly. Could someone point out what I'm missing? Thanks! http://codepad.org/6mK3qom2
I think the problem lies with the fact that even after a possible solution is found, the for loop keeps running. Thus whether possible_integer is ultimately increased depends on whether the last attempt at finding a solution worked or not (If it doesn't work your else statement resets possible_integer to 0 and that is all the information your while loops receives. Thus preventing the incrementation of possible_integer even if multiple solutions were found along the way.). To prevent this from happening, I would separate the while loop from your function. Then change the function to stop and return a value such as "True" if a solution is found (e.g., if small*a + medium*b + large*c == n: return True). Your else statement can then be paired with your outer for loop. Your else statement can return a value such as "False." If this value is returned by the function, then you can reset possible_integer to 0. This will ensure that possible_integer is reset only if no solutions are found. Good luck.
Note that it is important not to pair your else statement with the if statement when using the approach I suggested. Doing so would end the for loop every time a solution was not found on the first try (which would be a lot of the time) and prevent all possible solutions from being tested.
Note to cb12: you should use codepad too when giving a detailed answer, because it's very difficult to read otherwise.
thanks very much cb12 after working through your reply i fixed my code and it's now running smoothly!
Join our real-time social learning platform and learn together with your friends!