[Tutor] endless loop

Alan Gauld alan.gauld at btinternet.com
Mon Jul 5 19:36:46 CEST 2010

"prasad rao" <prasadaraon50 at gmail.com> wrote

> def rr(z,m=1):
>          q=lambda n:m%n==0
>          s=lambda False : 0

This is always false???

>          a=filter(s,map(q,range(1,z)))

So this is always empty?

>          if not a:

So this is always true

>              m+=1
>              rr(z,m)

So you contuinuaslly call rr with the original z and an increasing m.
But nothing about m terminates the recursion, so it recurses 
forever - or until you hit the recursion limit.

>          else:return m

This is never executed

> This code is going into endless loop.

Yep, I'd say so.

> Can some one show me  why it is going into Endless loop?

Because you wrote iit that way. This is one reason recursion is hard,
you must make 100% certain that there is a terminatin condition that 
will somehow get you out again...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list