Ex48 - Getting UnboundLocalError

Hi,

I am getting the below error and I am stuck in this. Could you please guide me how to fix this?

sam@sam-Inspiron-5720:~/mine/allprojects/ex48/skeleton$ nosetests
E
======================================================================
ERROR: tests.lexicon_tests.test_directions
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python**3.5/**dist-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/sam/mine/allprojects/ex48/skeleton/tests/lexicon_tests.py", line 6, in test_directions
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
UnboundLocalError: local variable 'assert_equal' referenced before assignment

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (errors=1)
sam@sam-Inspiron-5720:~/mine/allprojects/ex48/skeleton$ 

I have the same lexicon.py code of yours too and ran it. Still I am getting the same error.
Also, the distribute is pointed to python3.5. I tried to update it for python3.6.
Below is my lexicon.py code for the tests.

lexicon = {
    "north": 'direction',
    "south": 'direction',
    "east": 'direction'
}

def scan(sentence):
    results = []
    words = sentence.split()
    for word in words:
        word_type = lexicon.get(word)
        results.append((word_type, word))
    return results

Note: If i run the first test line " assert_equal(lexicon.scan(“north”), [(‘direction’, ‘north’)])" alone, it works fine.

The error appears when I try to run the full block as below.

def test_directions():
    **assert_equal(lexicon.scan("north"), [('direction', 'north')])**
    result = lexicon.scan("north south east")
    assert_equal = (result, [('direction', 'north'),
                             ('direction', 'south'),
                             ('direction', 'east')])

Please guide me to fix this, Thank you very much!

Hello samarasam,

I don’t personally use nosetests but it seems like you are using assert_equal wrong.

def test_directions():
    assert_equal(lexicon.scan(“north”), [(‘direction’, ‘north’)]) # <--- Here
    result = lexicon.scan(“north south east”)
    assert_equal = (result, [(‘direction’, ‘north’), # <--- And Here
    (‘direction’, ‘south’),
    (‘direction’, ‘east’)])

You seem to be using it differently in two different places. One place as a function and another as a variable which might be confusing python.

Now since i don’t use nosetests, i might just be confused about the way to assert something but i would recommend you switch the line to what Zed has on the website and see if that fixes it.

assert_equal(result, [(‘direction’, ‘north’),
                      (‘direction’, ‘south’),
                      (‘direction’, ‘east’)])

Thanks,
Coldsun1

Hey @samarasam you’ll see @coldsun1 is right. You’re calling the function assert_equals() by trying to assign to it. You should probably go and look at all the other places you use this function and make sure you’re not making the same mistake.

Also, when you post code, do it like this:

[code]
print("I am python code.")
[/code]

I edited your post to show you how to do it.

@coldsun1 @zedshaw
Thank you very much for helping on this!