1

I tried run this code but I have an error:

a4 local variable referenced before assignment

a1 = '/fr/store/a'
try:
   a2 = '/fr/store/b/collection/3432'
   a2_store = "store"
   a3 = a2.split ("store/")
   a4 = a3[0]
   if a2_store in a2:
      print ('a2')
except:
   if a4 not in a1:
      print ('a1')
Pavel
  • 5,374
  • 4
  • 30
  • 55
onesie1234
  • 19
  • 4
  • I didn't get an error running your code. I got `a2` printed. Please provide a [mre]. You can [edit] the question. BTW welcome to SO! Check out the [tour] and [ask] if you want more advice. – wjandrea May 19 '20 at 17:27
  • 1
    I'm not sure if it's part of the problem, but a [bare `except` is bad practice](https://stackoverflow.com/a/54948581/4518341). Try switching it to `except IndexError` and see if the issue continues. – wjandrea May 19 '20 at 17:29

1 Answers1

1

If an exception is raised before a4 = a3[0] is executed (for example, if a3[0] raises an IndexError), then a4 is, indeed, not defined.

chepner
  • 497,756
  • 71
  • 530
  • 681