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
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.
You can use the "break" function to end any for or while loop.
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.
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
Join our real-time social learning platform and learn together with your friends!