Prime number in Python
#Write your code below this line ๐
def prime_checker(number):
for num in range (2, number):
if num % number == 0:
print("It is not a prime number")
else:
print("It is a prime number")
#Write your code above this line ๐
#Do NOT change any of the code below๐
n = int(input("Check this number: "))
prime_checker(number=n)
How can I print a text that number is prime or not only once?
Solution 1:
Fix
number % num == 0
and not num % number == 0
A number isn't prime the moment you find a number that doesn't divide it, but only when you have tested all and none divides it
Use for/else
construction, it goes into else
if no break
has been used
def prime_checker(number):
for num in range(2, number):
if number % num == 0:
print("It is not a prime number")
break
else:
print("It is a prime number")
Note that this only fixes your way to do, but that isn't the optimal way to check if a numbre is a prime one, at least, ending range at square root of number, and directly verifying division by small numbers like 2,3,5,7