0

I have some code which gives me the answer I want, but I'm having trouble stopping it once I get the answer I want.

Here's my code:

multiples = range(1,10)
n = 1
while n>0:
    for i in multiples:
        if n%i!=0:
            n = n + 1
            continue
        elif n%10==0:
            print(n)

This is an attempt at solving problem 5 of Project Euler. Essentially, I'm supposed to get the smallest multiple of all the digits within a given range.

Now, when i run the above code using the example given (1-10 should yield 2520 as the smallest multiple), i get the answer right. However, the code continues to run infinitely and print the answer without breaking. Also, the moment I add the break statement to the end like so:

multiples = range(1,10)
n = 1
while n>0:
    for i in multiples:
        if n%i!=0:
            n = n + 1
            continue
        elif n%10==0:
            print(n)
            break

The code just keeps spamming the number 30. Any ideas why this is happening. For the record, I'm not really looking for an alternative solution to this question (the goal is to learn after all), but those are welcome. What I want to know most of all is where I went wrong.

  • You have nested loops. You are only breaking out of the inner loop while the outer loop runs indefinitely with no break condition being met. You can solve it with this: https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops – MaxYarmolinsky Nov 18 '20 at 00:44

3 Answers3

2

You never break out of your while loop. The for is the entire while body. break interrupts only the innermost loop; you have no mechanism to leave the while loop. Note that your continue doesn't do anything; it applies to the for loop, which is about to continue, anyway, since that's the last statement in the loop (in that control flow).

I can't really suggest a repair for this, since it's not clear how you expect this to solve the stated problem. In general, though, I think that you're a little confused: you use one loop to control n and the other to step through divisors, but you haven't properly tracked your algorithm to your code.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

One way to deal with this is to have an exception. At best a custom one.

multiples = range(1,10)
n = 1


class MyBreak(Exception):
    pass

while n>0:
    try:
        for i in multiples:
            if n%i!=0:
                n = n + 1
                continue
            elif n%10==0:
                print(n)
                raise MyBreak()
            
    except MyBreak:
        # now you are free :)
        break
    


Vlad
  • 1
  • 3
0

With this brake you stop only for loop, to exit whole cycle you should create trigger variable, for example:

multiples = range(1,10)
n = 1
tg = 0
while n>0:
    for i in multiples:
        if n%i!=0:
            n = n + 1
            continue
        elif n%10==0:
            print(n)
            tg = 1
            break
    if tg != 0:
        break

Or it'll be better to use a function and stop a cycle by return:

def func():
    multiples = range(1,10)
    n = 1
    while n>0:
        for i in multiples:
            if n%i!=0:
                n = n + 1
                continue
            elif n%10==0:
                print(n)
                return n