1

I would like to access the testing variable in main from testadder, such that it will add 1 to testing after testadder has been called in main.

For some reason I can add 1 to a list this way, but not variables. The nonlocal declaration doesn't work since the functions aren't nestled.

Is there a way to work around this?

def testadder(test, testing):
    test.append(1)
    testing += 1

def main():
    test = []
    testing = 1
    testadder(test, testing)
    print(test, testing)

main()
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Uninvolved
  • 15
  • 1
  • 5
  • Can you elaborate on what `test` and `testing` should be in general? It's a bit hard to understand what you want to do. – Mike Tung Sep 16 '16 at 13:39

1 Answers1

1

Lists are mutable, but integers are not. Return the modified variable and reassign it.

def testadder(test, testing):
    test.append(1)
    return testing + 1

def main():
    test = []
    testing = 1
    testing = testadder(test, testing)
    print(test, testing)

main()
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251