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
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.