A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. maitre_kaio
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Note to cb12: you should use codepad too when giving a detailed answer, because it's very difficult to read otherwise.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks very much cb12 after working through your reply i fixed my code and it's now running smoothly!

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.