A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

cannot figure out how to end my loop. i see that the last line is within the main FOR loop, so that's why it's printing. do i need to add a while loop? i'm stuck. n=raw_input ("Enter # to test: ") n=int(n) for c in range (0,n): remainder = n-(20*c) for b in range (0,n): a = remainder-(9*b) a = a/6 if 6*a + 9*b + 20*c == n: if a>=0 and b>=0 and c>=0: print n," can be bought",a,b,c print n," cannot be bought" if i enter 6, i get: Enter # to test: 6 6 can be bought 1 0 0 6 cannot be bought # don't want this last line to print

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

    Your print statement is treated as coming after all the other elements in the program. You could try adding an else statement after your final if and then printing The else statement needs the same indentation as your final if statement then indent the print under the else. This should have your print statement running when no solution is found.

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

    You can use the "break" function to end any for or while loop.

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

    You need an else which goes with the if 6*a + 9*b + 20c==n: else print n, "cannot be bought" Not familiar with this language but that is what you need.

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

    when i add the else clause to the if 6*a..., it prints n,"cannot be bought" each time it completes a loop. so when i try n=6, i get: 6 can be bought 1 0 0 6 cannot be bought 6 cannot be bought ... i can't figure out a way for it to stop once it finds that n CAN be bought

  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.