-1

I got the error "UnboundLocalError: local variable 'start_time' referenced before assignment" when running the code below. How can I solve this issue?

I use Spyder (Python 2.7), Windows 7 Ultimate. My full code line here

  • 1
    Post the full traceback, I don't see `start_time` being referenced before it is assigned in the code you posted. The error means at some point you set start_time to something, but before that happened you tried to reference it, which isn't possible because it doesn't exist yet. – MyNameIsCaleb Apr 22 '19 at 17:43
  • Possible duplicate of [Python variable scope error](https://stackoverflow.com/questions/370357/python-variable-scope-error) – MyNameIsCaleb Apr 22 '19 at 17:44
  • Hi ! I have posted again my full code in the link above, can you please check it.Thank you – Tri Nguyen Apr 23 '19 at 10:17
  • Unindent line 326 and that will fix it I think – MyNameIsCaleb Apr 24 '19 at 01:58

1 Answers1

0

Since there is no code:

exception UnboundLocalError
Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable. This is a subclass of NameError. [reference]

So in this example:

counter = 0

def count():
    counter += 1
    return counter

count()

You would get an UnboundLocalError because even though counter is assigned up top, within the scope of the function (locally) it is not. It is referenced before it has been assigned.

Likely in your code, start_time is being evaluated or added to or something else before it has actually been assigned, i.e. before it holds any value.

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • Thanks to your comment! As the link attached in my question, could you help me to find out where I should amend the code? thank you – Tri Nguyen Apr 24 '19 at 00:01