Ucode2
Ucode2

Reputation: 82

Finding whether a number is prime or not python3

I have written a code to find out if a number is prime or composite. The code works fine when I input a prime number but when I input a composite number the output is:

enter number: 100
The number is not prime.
The number is prime.

I don't want The number is prime output for composite number input. Here is my code:

print ('This program tells whether the number is prime or not')
print ('')

def prime(x):
  if x < 2:
    print('The number is not prime.')
  else:
    for n in range(2, x - 1):
      if x % n == 0:
        print('The number is not prime.')
        break
    print('The number is prime.')

i = input('enter number: ')
prime(int(i))

Please tell me what can I do to correct it.

Upvotes: 1

Views: 441

Answers (6)

Moses Ogbonna
Moses Ogbonna

Reputation: 1

def prime(x):
  is_prime = True
    for i in range(2, x):
      if x % i == 0:
        is_prime = False
  if is_prime:
    print("It's a prime number.")
  else:
    print("It's not a prime number.")   


n = int(input()) # Check this number
prime_(number=n)

Upvotes: 0

JRudransh
JRudransh

Reputation: 105

This is the recommended way to solve this problem.
do not use hard coded print statement.
try to return True or False instead.

def is_prime(x:str):
  if x < 2:
      return False
  else:
      for n in range(2, int(x/2)): # Use this for more speed
          if x % n == 0:
              return False
      return True

Now you can check the number is prime or not by calling this is_prime function

print('Number is prime' if is_prime(6) else 'Number is not prime')

Upvotes: 1

lone_ranger
lone_ranger

Reputation: 75

I can see why. you are missing else after if. try this:

print ('This program tells whether the number is prime or not')
print ('')

def prime(x):
  if x < 2:
    print('The number is not prime.')
  else:
    for n in range(2, x - 1):
      if x % n == 0:
        print('The number is not prime.')
        break
    else:
        print('The number is prime.')

i = input('enter number: ')
prime(int(i))

Upvotes: 1

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

if num > 1:
   for n in range(2, x-1):
      if x % n == 0:
         print('The number is not prime.')
         break
   else:
      print('The number is prime.')
else:
    print('The number is not prime.')

Simply fix that indentation in the for loop. Also, this looks a lot cleaner.

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71620

The problem is the indentation, you've to move the indentation of the last line and add a break after that, so try using:

print ('This program tells whether the number is prime or not')
print ('')

def prime(x):
  if x < 2:
    print('The number is not prime.')
  else:
    for n in range(2, x - 1):
      if x % n == 0:
        print('The number is not prime.')
        break
      print('The number is prime.')
      break

i = input('enter number: ')
prime(int(i))

Upvotes: 3

Tom Ron
Tom Ron

Reputation: 6181

The problem is that when you break the loop the last print statement is called. If you end the function using return statement you will not reach the last print statement.

def prime(x):
  if x < 2:
      print('The number is not prime.')
  else:
      for n in range(2, x - 1):
          if x % n == 0:
            print('The number is not prime.')
            return
      print('The number is prime.')

Upvotes: 0

Related Questions