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,260 points
5,094 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
• 58,020 points

selected Oct 14, 2019 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, 2019 by Anand Kumar
Thanks a lot Anand, this solved my problem. Upvoting your answer right away... :)

Related Questions In Python

0 votes
1 answer

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

n = int (input ('ENTER NUMBER TO ...READ MORE

answered Jul 16, 2020 in Python by amrut
• 240 points
1,070 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, 2019 in Python by Nymeria
• 3,560 points
11,286 views
0 votes
2 answers

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

a = int (input('enter number')) num = a sum ...READ MORE

answered Jul 16, 2020 in Python by amrut
• 240 points
1,773 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 14, 2019 in Python by Sheik janibasha
5,387 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

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

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
+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
• 58,020 points
23,089 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
• 58,020 points
862 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP