Check a number is Prime or not in Python

0 votes

def main():

n = input("Please enter a number:")

def is_prime(a):
    x = True 
    for i in (2, a):
            while x:
               if a%i == 0:
                   x = False
                   x = True

    if x:
        print "prime"
        print "not prime"


If the entered number is not a prime number, it displays "not prime", as it is supposed to, but if the number is a prime number, it doesn't display anything. Could you please help me with it?

Oct 26, 2018 in Python by findingbugs
• 3,200 points

2 answers to this question.

+2 votes
Best answer

There is an efficient way to write the same program. 

The loop you wrote can be concisely rewritten in Python:

def is_prime(a):
    return all(a % i for i in xrange(2, a))

That is, a is prime if all numbers between 2 and a (not inclusive) give non-zero remainder when divided into a.

answered Oct 26, 2018 by Priyaj
• 57,530 points

selected Oct 14, 2019 by Omkar
+2 votes
number = int(input("Enter the Number"))
    print(number,"is not prime Number")
    print("Oh! Enter the natural number to check the prime or not")
    check =number//2
    for i in range(2 ,check+1):
        if check%i==0:
            print("oh! sorry",number,"is not prime Number")
        print("congratulation!",number,"is the Prime Number")
answered Oct 9, 2019 by Anand Kumar
Thanks a lot Anand, this solved my problem. Upvoting your answer right away... :)

Related Questions In Python

0 votes
0 answers

What is the logic to check if a number is prime or not in python?

I am looking for a function with ...READ MORE

Aug 2, 2019 in Python by Waseem
• 4,530 points
0 votes
1 answer

How do I check if input string is a valid regular expression or not in Python?

Hi. Good question! Well, just like what ...READ MORE

answered Feb 12, 2019 in Python by Nymeria
• 3,540 points
0 votes
1 answer

What is the python logic to check if a number is armstrong number or not?

number = int(input("enter a number")) s = 0 temp ...READ MORE

answered Jul 30, 2019 in Python by Mohammad
• 3,230 points
0 votes
2 answers

How to check whether a string contains alphabets or number in Python?

By using isAlpha () to check whether ...READ MORE

answered Jul 13, 2019 in Python by Sheik janibasha
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 963 views
0 votes
0 answers
+4 votes
6 answers
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 57,530 points
0 votes
1 answer

Is there a label/goto in Python?

No, Python does not support labels and ...READ MORE

answered Aug 1, 2018 in Python by Priyaj
• 57,530 points