0

I'm having an issue with this simple counter program:

import time
counter = 0

def countup():
  while counter < 10:
      counter += 1
      print counter
      time.sleep (1);
  countdown();

def countdown():
  while counter > 0:
      counter -= 1
      print counter
      time.sleep (1);
  countup();

countup();

And the error I am getting is:

UnboundLocalError: local variable 'counter' referenced before assignment

Does this mean that I need to define a counter variable inside each function? I have tried this, but it didn't work. I also tried a global variable at the start using: global counter = 0 but that didn't work either.

Apologies foor the noob question, I'm just a guy prodding a bit of python code.

0 Answers0