Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am using env python 3.8.2 I can't get the value var in return

def a(x):

    def b():

        nonlocal x

        if x>2:

            print(x)

            return x

        x = x + 1

        b()

    return b

print(a(1)())

The result I am getting is:

4

None

I want to return value of x, but actually, it is None, even though the value printed is 4

1 Answer

0 votes
by (36.8k points)

Return b(). for the first time you will call b, it reaches b() but returns nothing, so your final return value is None.

def a(x):

    def b():

        nonlocal x

        if x>2:

            print(x)

            return x

        x = x + 1

        return b()

    return b

print(a(1)())

> 3

> 3

 Improve your knowledge in data science from scratch using Data science online courses

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 31, 2020 by blackindya (18.4k points)

Browse Categories

...