0

Why is my function returning null value? My code takes 2 inputs a and b and gives the common smallest prime number and if it does not have any common smallest prime number then it will return -1 example input 3, 6 output 3 input 3, 7 output -1

def checkprime(ans,i):
    if (ans <= 2): 
        if(ans == 2):
            return True
        else:
            return False
    if (ans % i == 0): 
        return False
    if (i * i > ans): 
        return True 
    return checkprime(ans, i + 1) 

def commomdivisor(a,b,ans):

    if ((a*b)+1<ans):
        return -1
    if(a%ans==0 and b%ans==0):
        i=2
        if(checkprime(ans,i)== True):

            print("type",type(ans))
            fans=ans
            return int(fans)
        else:
            commomdivisor(a,b,ans+1)
    else :
        commomdivisor(a,b,ans+1)


a,b= map(int, input().split())
#b=int(input())
ans=2
print("type",type(ans))
print(commomdivisor(a,b,ans))
Ruan
  • 219
  • 5
  • 9
  • 2
    Python functions return `None` if nothing is explicitly returned. In your `else` closures, you are just calling the functions but not returning what you get from them. So add `return` before `commomdivisor` calls. – Yevhen Kuzmovych Oct 25 '19 at 13:44
  • Function is returning `None` because you didn't put `return` statement in `else` in `commomdivisor` function. – Prudhvi Oct 25 '19 at 13:45
  • There are a couple of (logic) paths in `commomdivisor` that do not have a return statement. – wwii Oct 25 '19 at 13:46

0 Answers0