Skip to content
Advertisement

For loop to get prime numbers [closed]

I’m at the very beginning with Python so my knowledge is quite noob level.

Anyway, I have found this example online:

JavaScript

Here the result from the example:

JavaScript

Unfortunately by running that code on Jupyter it returns:

JavaScript

I can’t really find the error here, any advice?

Advertisement

Answer

I guess your attempt was

JavaScript

whereas the correct code must be like

JavaScript

Note (i) different indentation level for else clause, and (ii) no second break. You might want to be more careful about indentation in python.

Here, else belongs to for, not if (and this distinction is realized by the indentation). For this, this documentation might be helpful. In summary, for ... else behaves like this: once the for loop ends, else part is executed, unless the loop was terminated by a break.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement