0

I've made a function that checks prime number, it's running fine but i don't know why it's showing None after the result.

Here is the code -

num = int(input('Enter a number:'))

def prime_no_check(num):
  if num>1:
    for i in range(2,num):
      if num%i ==0:
        print(num, 'is not a prime number')
        break
      else:
        print(num, 'is a prime number')
  else:
    print(num, 'is not a prime number')

print(prime_no_check(num))

Output:

Enter a number:7
7 is a prime number
None

Please help me find where the issue is in the code.

ChasedByDeath
  • 185
  • 3
  • 15
  • 3
    `print()` only prints to the console. It does not return anything (to be specific it returns `None`). If you don't `return` anything from the method - its return value will be `None` – rdas Apr 21 '20 at 09:12
  • Your function already prints to screen. Your function prints to screen and then returns the value None. You should just call your function like `prime_no_check(num)`. There is no need for the print function on the last line. – Rajarishi Devarajan Apr 21 '20 at 09:48
  • @RishiDev Can you please explain with a solution code, as i`m still not getting it. – Akarshan narang Apr 21 '20 at 14:34
  • Replace this line `print(prime_no_check(num))` with `prime_no_check(num)`. This is because your function `prime_no_check` already prints the result you require, and it returns `None`. So there is no need for any additional `print()` functions. – Rajarishi Devarajan Apr 21 '20 at 15:05

0 Answers0