Search code examples
pythonloopsfor-loopprimes

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

  • There are few mistakes in your code. I have modified them.

    def prime_checker(number):
        for num in range(2, number):
            if number % num == 0:
                print('Not prime')
                return
        print('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)
    

    For loop is to check if any of the number starting from 2 is a factor of number or not.