6.is_prime, Your function fails on is_prime(9)


#1

heres my code:
def is_prime(x):
__if x<2:
____return False
__elif x ==2 :
____return True
__for n in range(2, x):
____if x % n ==0:
______return False
______break
___else:
_____return True

print is_prime(9)


#2

Your for loop only runs once now, go ahead, and add a print statement after your for loop:

for n in range(2, x):
    print(n)
//rest of your code

what is happening? When return is reached, the function ends. In your first run of the loop, either the if condition is true, or it will go to else. A return is reached either way, something is returned, and the function ends. How to solve this? Well, you could simple remove your else, put return True on the same line as your for loop, then it will loop through all values, if the number is not a prime, the if statement will be true at certain point, return false, the function (and the loop obviously) ends, problem solved :slightly_smiling:


#4

@l1palas,
for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard.


#6

No need to use break, return will end the function if if evaluates to true.


#7

just remove the indent before the last else in your code so that you can see the else statement alligned with the for statemen .
i short make it a for/else statement.


#9