There are many efficient ways to check if a number is prime.
Here, What is the best algorithm for checking if a number is prime?
However specific to your questions, there are a lot of errors:
1) The code fails due to Zero Divison Error because your range starts from 0, try
for in range(2, num)
since num %1 is always zero
2) if(num <= 1):
should be outside for loop to avoid unnecessary looping
Here is the simple modified version of your script:
num = int(input("Enter any number : "))
def check_prime(num):
if(num <= 1):
print("Please enter a number more than 1")
return
# check if number is 2 or 3 as they are prime numbers
if (num == 2 or num == 3):
print("Number is prime")
return
# checking for number divisible by 2, 3 or 5 covers large set of numbers
if (num % 2 == 0 or num % 3 == 0):
print("Number is not a prime number")
return
# if none of the above case is satisfied then iterate to half of the number
# as number more than half is never a factorial of the number
for i in range(7, int(num / 2)):
if(num % i == 0):
print("The number is not a prime number")
return
print("Number is prime")
check_prime(num)
However, there are efficient solutions available.