Python Forum
UnboundLocalError: local variable 'a' referenced before assignment
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnboundLocalError: local variable 'a' referenced before assignment
#1
Hi again,
I am working on more homework. I have ideas of how to solve the problem but I am running into a bug I don't understand

def not_bad(s):
    t = s.split()
    for x in t:
        if x == "not":
            a = t.index(x)
        if x == "bad":
            b = t.index(x)
        if a < b:
            newlist = [range (a, b)]
            del t[newlist]
    print (t)




    return t


print (not_bad("this is a test"))
Basically just trying to determine if one word shows up before another in a sentence but when I run that test it gives me back:

Traceback (most recent call last):
File "/Users/leon/PycharmProjects/playpen/test5.py", line 19, in <module>
print (not_bad("this is a test"))
File "/Users/leon/PycharmProjects/playpen/test5.py", line 8, in not_bad
if a < b:
UnboundLocalError: local variable 'a' referenced before assignment

I feel like a got assigned in the if statement.

Thanks as always.
Reply
#2
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Well what happens if x is not equal to "not" ? then a is never assigned. also b is only assigned when x is equal to "bad". only one of these two variables will be assigned at a time. So that your comparison of a < b fails. Try to assign some value to both variables. if you want to compare these to at any given point then declare them outside of your loop to store them for every iteration
def not_bad(s):
    t = s.split()
    a = 0
    b = 0
    for x in t:
        if x == "not":
            a = t.index(x)
        if x == "bad":
            b = t.index(x)
        if a < b:
            newlist = [range (a, b)]
            del t[newlist]
    print (t)
    return t
print (not_bad("this is a test"))
maybe you ment something like this? what was the task exactly?
Reply
#4
(May-23-2018, 04:21 PM)fad3r Wrote:
def not_bad(s):
    t = s.split()
    for x in t:
        if x == "not":
            a = t.index(x)
        if x == "bad":
            b = t.index(x)
        if a < b:
            newlist = [range (a, b)]
            del t[newlist]
    print (t)

a only exists if x=="not". You can't reference something that doesn't exist.
As was previously mentioned, you can fix this by simply defining your variables before hand with default values.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rounding exercise: UnboundLocalError: local variable referenced before assignment Drone4four 5 3,337 Sep-06-2020, 09:01 AM
Last Post: ibreeden
  why am I getting "local variable 'x' referenced before assignment"? wlsa 6 9,066 Jun-16-2018, 05:31 PM
Last Post: buran
  variable referenced before assignment Niko047 4 22,898 Aug-04-2017, 07:55 PM
Last Post: nilamo
  local variable 'l' referenced before assignment... darkreaper1959 4 7,375 Jan-21-2017, 08:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020