简体   繁体   中英

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.

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))

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))
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.

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')

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.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM