Function-doubt(practice problems)

Python Program to Check weather a Given Number is Prime or Not.

def prime(n):
if n==1:
return “not a prime number or composite number”
elif n>1:
for i in range(2,n):
if n%i==0:
print(“stmt is not a prime number”)
return “prime number”

(why above program doesn’t show result )

The above function doesn’t return anything when the number is not prime. it returns for prime numbers only. printing is not the same as return.
also, it doesn’t handle the case of 2. when the number is 2, within the range function, it goes range(2,2) which is None. that case fails. It also doesn’t manage the case of negative numbers.

Try to solve these issues and get back to us if you need help. :slight_smile:

1 Like