-1
   stack = ['','','','','','']
   global first
   global last
   first = 0
   last = 0
   print(stack)
   while True:
       def push():
           if 0 <= last:
               stack[last] = 'D'
               last = last + 1
               if last == 6:
               last = -1
           else:
               print("The stack is full.")
               print(stack)
        def pop():
           stack[last] = ''
           last == stack[last-1]
           print(stack)
           if first == last:
               print("The queue is empty")
       a = input("Push or pop. ")
       if a == 'push':
           push()
       elif a == 'pop':
           pop()

I've assigned last as a global variable yet I get this error UnboundLocalError: local variable 'last' referenced before assignment. Considering I've made last a global variable I don't understand why the error is suggesting otherwise, any solutions?

Joseph
  • 41
  • 5
  • 2
    Don't define functions inside loops. Don't use `global`. Don't preallocate lists. – Daniel Nov 24 '16 at 18:38
  • I know the use of global variables is bad practice, but why can't it call the global variable from within the loop? @Daniel – Joseph Nov 24 '16 at 18:43

1 Answers1

-1

The global must be inside the def. Using global variables in a function other than the one that created them

Community
  • 1
  • 1
Waxrat
  • 2,075
  • 15
  • 13