No binding for nonlocal 'example' found - Please help

I am a pretty beginner in python, after completing a course from udemy I started to make a bank, it allows you create accounts, deposit money, invest in schemes etc etc. Mycode is more than 120 lines of now and there’s a lot of functions. My problem is that, whenever I use a nonlocal statement in order to bring in values from another function, it gives me an error - Syntax Error - No binding for nonlocal ‘example’ found. My code is written below:

Here is the code where the variables are present:

if (Answer == "1"):
                    print("Great! So you have chosen to open a standard account with our bank! It will cost you 400 USD. Please answer the following the questions to create your account.\nIf this was a mistake, type 'Back' in the place of your name.")
                    NameStandard = input("Type your full name:")
                    if (NameStandard == "Back" or NameStandard == "back"):
                         NameStandard = 0
                    AgeStandard = input("Type your age:")
                    EmailStandard = input("Type your email address:")
                    # Standard Created profile Info

                    print("Your profile has been created! Your details are provided below:\nName: " + str(NameStandard) + ".")
                    print("Age: " + str(AgeStandard) + ".")
                    print("Email Address: " + str(EmailStandard) + ".")
                    print("Your account code is " + str(Code))
                    print("Please keep your personal code hidden, it will be used for accessing your account later.")

Here is the code in which I have used the nonlocal statements,

elif (F == "1" and AccountCreated == True and StandardAccountCreated == True):
            nonlocal NameStandard
            nonlocal AgeStandard
            nonlocal EmailStandard
            nonlocal Code
            print("Name: " + str(NameStandard) + ".")
            print("Age: " + str(AgeStandard) + ".")
            print("Email Address: " + str(EmailStandard) + ".")
            print("Your account code is " + str(Code))
            print("Please keep your personal code hidden, it will be used for accessing your account later.")

The second piece of code is like 70 lines below the first, but it shows an error whenever I try to access the 2nd piece of code. What is my problem here:/?

Just those two code parts are not enough to be sure what’s the issue, because it’s not visible what are scopes of those code parts. Perhaps those variables are not defined in some outer function, but in the global scope.

Something like this will work:

def outside_func():
    def inside_func():
         nonlocal variable
         print(variable)  # prints: 1
    variable = 1

But this will not:

another_variable = 2
def another_func():
     nonlocal another_variable  # SyntaxError
1 Like

My code seems to be similar to your first part, so I guess I am making some other mistake here. Should I send the entire file instead? Its 192 lines.

You could put it on github, replit or similar page.

1 Like

“nonlocal” only works in nested functions. Your code-snippet doesn’t show any functions, so no idea if you used it correctly.
It’s possible you might want to use the “global” keyword instead, assuming you don’t actually have nested functions, but merely more functions.

That being said, you don’t actually want to use “global” EVER - because this is just an error waiting to happen.
Instead you would create a Class, but that’s quite some advanced stuff.
I mean, for a start you could also just create a global object outside of all functions and use the functions to change that. Or just include the object in the function-call as variable.

1 Like

@AimanFayaz as @Jagaya said I do not see any functions either. It would be better to post your full code on Github so that we may look at it as @sanity suggested.

About the error you are receiving, Python is searching for the a variable within the enclosing scope, which is the scope of the parent function. Since the scope of the parent function doesn’t have the variable Python doesn’t look further in the global scope, it issues an error.

my_var = 'Hello world again'

def parent_func():
    '''Example of incorrect usage of nonlocal'''

    def child_func():
        nonlocal my_var # Error is thrown.
        my_var = 'Hello world again'



def parent_func2():
    '''Example of correct usage of nonlocal'''

    my_var2 = 'Hello world'

    def child_func2():
        nonlocal my_var2
        my_var2 = 'Hello world again'



1 Like

@Jagaya @brandon_wallace I have posted my entire code on replit, can you check out my code there?

If you can give us a link to the code, I’ll check it out ^^

Aight, here is the link:

Thanks a lot!

So I copied the code and tried a couple things and the error is actually quite simple.
You never declared the “nonlocal” variables in the parent function.

The keyword “nonlocal” (casesensitive → you uppercased it at one point) means the interpreter will look into the parent function and for the value of the variable. It will NOT CREATE a new variable. So all you have to do is literally just declare the nonlocal variables with anything. As Python is not type-sensitive, doesn’t really matter.

I just declared the variables in MainMenu(), fixed the uppercase and the code works without any errors :wink:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.