Python Forum
Lambda function recursion error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lambda function recursion error
#1
So basically i am having trouble with the following Code. Whenever i run it, in the 3rd iteration the programm throws a RecursionError.
lst=[[]]

while len(lst)<10:
    lst.append([])
    lst[-1].append(lambda x: x+5)
    
    for i in range(1, int(len(lst)/2+0.5)):
        for a in lst[i]:
            for b in lst[-1-i]:
                value1=a(b(100))
                if value1>0 and value1%1==0:
                    lst[-1].append(lambda x: a(b(x)))
I copied this out of a project and i just need to understand, why it doesn't work or how i can fix it. In the project the lambda function adds, subtracts, divides and multiplies the x value, hence the if-statement.

The error occurs in the 3rd last line (value=a(b(100)) and if i delete that one and the following line it works.
The error is not caused by the while-loop being iterated too many times.

Thanks in advance for your responses.
Reply
#2
My guess is this:
if value1>0 and value1%1==0
I don't see how this will ever be False. You are adding integers, so value1%1 == 0 is always True, and I don't see how x is ever going to go negative, so x+5 is never going to be negative.

This code may work with a different lambda, but I don't see it working with the lambda you provide.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 849 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Max recursion depth.... Error MeloB 2 1,838 Feb-16-2022, 05:21 PM
Last Post: MeloB
  Writing a lambda function that sorts dictionary GJG 1 1,986 Mar-09-2021, 06:44 PM
Last Post: buran
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,121 Dec-14-2020, 05:45 PM
Last Post: karabakh
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,689 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  translating lambda in function fabs 1 2,121 Apr-28-2020, 05:18 AM
Last Post: deanhystad
  why this error occured in recursion ashishraikwar 1 1,736 Apr-24-2020, 11:12 AM
Last Post: buran
  Lambda function error localsystemuser 3 4,659 Jan-29-2020, 01:43 PM
Last Post: DeaD_EyE
  matplotlib recursion error when repeatedly displaying a surface AdeIsHere 0 1,896 Sep-19-2019, 04:36 PM
Last Post: AdeIsHere
  Help with AWS Lambda function faws2019 0 1,556 Aug-27-2019, 01:54 PM
Last Post: faws2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020