Dictionary

ast none at gmail.com
Mon Feb 25 11:05:09 EST 2019


Le 24/02/2019 à 05:21, Himanshu Yadav a écrit :
> fibs={0:0,1:1}
> def rfib(n):
>        global fibs
>         if not fibs.get(n):
>                  fibs[n]=rfib(n-2)+rfib(n-1)
>          return fibs[n]
> 
> Why it is gives error??
> 

Nothing to do with the malfunction, but you dont need
to define fibs as global since you dont remap "fibs"
in side the function.

As explained by Peter, when n equals 0, not fibs.get(n)
will return True and recursivity goes on with n=-1, -2
until you reach the maximum allowed number of recursion
depth


More information about the Python-list mailing list