-1

So, I am trying to solve a problem which involves finding the maximum depth of a tuple (for example, depth(('a',('b','c','d'),('e','f','g'))) should return 2 and depth(('a', ('b',), ('c', ('d', ('e',), f), ('g', 'h', 'i')))) should return 4. I am trying to use recursion to solve the problem where I use a counter 'c' to keep track of depth of each tuple (within the main tuple). The problem is,I get the following error, local variable 's' referenced before assignment.

My code is something like:

s=0
def depth(tuple):
     do something
     s=s+1
     depth(some_nested_tuple)
     return

What am I doing wrong?

Janmajay
  • 51
  • 6

1 Answers1

0

If you want to do something with your global variable inside a function you have to do it like this:

s=0
def depth(tuple):
     # do something
     global s
     s=s+1
     return depth(some_nested_tuple)
pythad
  • 4,241
  • 2
  • 19
  • 41