Division using recursion in python -




i pretty sure must glaringly stupid mistake me. can explain wrong in division code using recursion. know there lot of alternatives need know wrong this

def division(a,b):     x = 0     if < b:         return x     else:         x += 1         return division(a-b,b)      return x 

when division(10,2), gives me 0 output

this might work little better you:

def division(a,b,x=0):     if < b:         return x     else:         x += 1         return division(a-b,b,x)      return x 

every time function ran through new recusion, setting x 0. way, defaults 0 if x not specified, , should work think want.

also note not work negative numbers, knew :)





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -