addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Beginner Python Quesiton

From: Stan S.
Sent on: Thursday, December 17, 2009 4:03 PM
Hi python programmers,

I have a beginner python question I hope someone can help me with.  I have defined a "sub" function in which I want to increment a local variable in the outer function, but when I run the code, I get this error.

  File "/Users/stan/Desktop/", line 94, in tests
    test(dd['one']['two'] == 12)    
  File "/Users/stan/Desktop/", line 83, in test
    i = i + 1
UnboundLocalError: local variable 'i' referenced before assignment
Process terminated with an exit code of 1

Here is my code

def tests():

    i = 0

    def test(aBoolean):
        i = i + 1
        if aBoolean:
            print(i, 'pass')
            print(i, 'fail')

    dd = DoubleDictionary()
    dd['one']['two'] = 12
    test(dd['one']['two'] == 12)    
    test(dd.has_keys('one', 'two'))
    test(not dd.has_keys('one', 'three'))
    test(not dd.has_keys('three', 'four'))
if __name__ == '__main__':

What am I not understanding??


Stan Silver

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy