Check a number is Prime or not in Python

0 votes

def main():

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

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


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

main()

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,180 points
522 views

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
• 56,940 points

selected Oct 14 by Omkar
+2 votes
number = int(input("Enter the Number"))
if(number==1):
    print(number,"is not prime Number")
elif(number==0):
    print("Oh! Enter the natural number to check the prime or not")
else:
    check =number//2
    for i in range(2 ,check+1):
        if check%i==0:
            print("oh! sorry",number,"is not prime Number")
            break
    else:
        print("congratulation!",number,"is the Prime Number")
answered Oct 9 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 in Python by Waseem
• 4,470 points
25 views
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 in Python by Nymeria
• 3,520 points
137 views
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 in Python by Mohammad
• 3,130 points
34 views
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 in Python by Sheik janibasha
75 views
+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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 350 views
+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
• 56,940 points
4,344 views
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
• 56,940 points
129 views