I had same code / problem. This helped me:
I believe this will help you out! All credit to Factoradic as well I didn't come up with this answer.
Is_prime for some reason doesn't work on number 9
Ok, so let us assume that x = 9, what happens in your code?
It starts with the if, but 9 < 2 is False so we go to the elif. 9 == 2 is False so we go to the else.
Now we have a for loop and the first value of i is 2.
The condition of if is 9 % 2 == 0, this is obviously False, but there is an else block, so we go there. And there is only a single statement - return True and this value will be returned.
In other words, your code will return True for every odd number that is > 2.
To solve this problem you can move return True outside the for loop.
Also from bibleman13
Also this from Stetim94 might help as well. Once again all credit to Stetim94 I didn't come up with this answer.
Is_prime: problem with "Your function fails on is_prime(9)"
a function ends the moment a return keyword is reached, in your case this happens in the first run of the loop.
You want to change the indent of else/return True, since you only want to return True after the loop has finished running