I am trying to write code to find whether a given number is odd or even using recursion in Python.
When I execute my code, the recursive function descends down to 0 correctly but then doesn't halt, and keeps going with negative values. I expect it to stop when it reaches 0.
How to make sure the function returns after it reaches zero?
My code:
def odeven(n):
if n%2 == 0:
print("even no. : ",n)
elif n%2 != 0:
print("odd no. : ",n)
elif (n == 0):
return 0
return odeven(n-1)
result = odeven(10)
print("result odd ={}".format(result))
Aucun commentaire:
Enregistrer un commentaire