# 6-is_prime

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
break command not working
for the FOR loop i declared n=2 before the loop starts and it throws error

Traceback (most recent call last):
File “python”, line 17, in
File “python”, line 9, in is_prime
ZeroDivisionError: integer division or modulo by zero

<What do you expect to happen instead?>

```python

Replace this line with your code.

``````    if x < 2:
print "%d is not a prime number" %x
return False
#break
else:
n=2
for n in range(x):
if x % n == 0:
print "%d is not a prime number" %x
return False
#break
else:
print "%d is a prime number" %x
return True

is_prime(1015)``````

#2

lets see:

``````def is_prime(x):
if x < 2:
print "%d is not a prime number" %x
return False
#break
else:
n=2
for n in range(x):
print n
if x % n == 0:
print "%d is not a prime number" %x
return False
#break
else:
print "%d is a prime number" %x
return True

is_prime(1015)
``````

run this code, as you can see, for the first iteration of the loop, `n` is zero. Dividing by zero result in an error

https://docs.python.org/2/library/functions.html#range

how you might be able to change the start value

#3

that is why I have declared n=2 before the for loop

#6

i saw, then i pushed you in the right direction in the hope you realized that wouldn’t work

`range()` produces a list, and will assign each value to the list to the loop iterator:

``````for x in range(5):
print x
``````

is perfectly valid code, without declaring `x` beforehand given `range()` will use the start value of the list (if `x` is defined, it will simple be overwritten)

#7

ok
and what about the break command,if I try to use it in a loop or conditional if statement, it keeps giving an error of unexpected identation even I am alligning the break loop to the if body.

#8

The new learning environment has still some indent issue, i then usually remove all indent and re-indent everything

why would you use `break`? `return` will end the function, so any code after return will never be executed, prove of concept:

``````def example():
return True
print "i will never be executed given i am after return"

example()
``````

#9

ok Thanks
some confusions are removed.

#10

by default, a function returns None at the end of the function, prove:

``````def example():
print "proof"

print example() # print what the function returns
``````

as you can see, it produces `None`

now we can make the function returns something else at the end of a function by using a return keyword.

so, the function ends the moment a return keyword is reached, even in loop, prove of concept:

``````def example():
for i in range(5):
print "iteration number: " + str(i)
return True

print example()
``````

which is why we don’t need `break`

#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.