LPTHW : Ex48 Assertion Error Lists Differ

I’m on EX48 (Advanced User Input) and have made the lexicon.py and placed it in the appropriate position in the directory, but when I run the nosetests it shows this Error from lexicon_tests.py file:

(I’m on Windows 7, using Powershell,
And my Editor is VSC)

FAIL: lexicon_tests.test_stops
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
  File "C:\Users\pp\desktop\python\core\n\ex481\ex48\lexicon_tests.py", line 30, in test_stops
    assert_equal(lexicon.scan("the"), [('stop', 'the')])
AssertionError: Lists differ: [('error', 'the')] != [('stop', 'the')]

First differing element 0:
('error', 'the')
('stop', 'the')

- [('error', 'the')]
?    ^^^ ^

+ [('stop', 'the')]
?    ^^ ^


----------------------------------------------------------------------
Ran 3 tests in 0.047s

FAILED (failures=1)

This is my lexicon.py file :-

lexicon = {

        'north': 'direction',

        'south': 'direction',

        'east' : 'direction',

        'west' : 'direction',

        'go'   : 'verb',

        'kill': 'verb',

        'eat' : 'verb',

        'stop': 'verb',

        'The': 'stop',

        'of': 'stop',

        'from':'stop',

        'at':'stop',

        'it':'stop',

        'Door':'Noun',

        'Bear':'Noun',

        'princess':'Noun',

        'cabinet':'Noun',

        '123456789': 'number',

        } 

def convert_numbers(s):

    

        try:

            return int(s)

        except ValueError:

            return s

def scan(sentence):

    words = sentence.split()

    result = []

    for word in words:

        check_string = convert_numbers(word)

        if word in lexicon:

            check_number = convert_numbers(word)

            pair = (lexicon[word], check_number)

            result.append(pair)

        elif type(check_string) ==type(1):

            number = convert_numbers(word)

            if number:

                pair = ('number', number)

                result.append(pair)

        else:

             error_word = word

             pair = ('error', error_word)

             result.append(pair)

            

    return result

And this is the lexicon_tests.py file:

from nose.tools import *

from ex48 import lexicon

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')])

test_directions()

def test_verbs():

    assert_equal(lexicon.scan("go"), [('verb', 'go')])

    result = lexicon.scan("go kill eat")

    assert_equal(result, [('verb', 'go'),

                          ('verb', 'kill'),

                          ('verb', 'eat')])

def  test_stops():

    assert_equal(lexicon.scan("the"), [('stop', 'the')])

    result = lexicon.scan("the in of")

    assert_equal(result, [('stop', 'the'),

                          ('stop', 'in'),

                          ('stop', 'of')])

def test_nouns():

    assert_equal(lexicon.scan("bear"), [('noun', 'bear')])

    result = lexicon.scan("bear princess")

    assert_equal(result, [('noun', 'bear'),

                          ('noun', 'princess')])

def test_numbers():

    assert_equal(lexicon.scan("1234"), [('number', 1234)])

    result = lexicon.scan("3 91234")

    assert_equal(result, [('number', 3),

                          ('number', 91234)])

def test_errors():

    assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])

    result = lexicon.scan("bear IAS princess")

    assert_equal(result, [('noun', 'bear'),

                          ('error', 'IAS'),

                          ('noun', 'princess')])

What Am I doing wrong?

"the" != "The"
:wink:

Thanks! Appreciate ‘the’ Help :slight_smile:

*I went through almost all the errors correctly but hit a snag on the last one, It says [‘tuple’ object is not callable]

    ======================================================================
    ERROR: tests.lexicon_tests.test_directions
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
        self.test(*self.arg)
      File "C:\Users\pp\desktop\python\core\n\ex481\ex48\tests\lexicon_tests.py", line 17, in test_directions
        ('direction', 'east')])
    TypeError: 'tuple' object is not callable

    ----------------------------------------------------------------------
    Ran 13 tests in 0.040s

    FAILED (errors=1)

What can I do to correct it?

It tells you where the error happened, so look at that line and the lines before that. If you can’t figure it out, post those lines in the latest version again and tell us where line 17 is (or post the whole file again but without all those newlines).

(There seems to be a stray test_directions() in the code you posted first, but I don’t see how that would cause that error.)

By the way, if you wrap your code in back ticks when posting, like so

```
# your code
```

we can read it better.

This is where the Error is coming from:

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')])


**And the lexicon.py file is this:**


lexicon = {

            'north': 'direction',

            'south': 'direction',

            'east' : 'direction',

            'west' : 'direction',

            'go'   : 'verb',

            'kill': 'verb',

            'eat' : 'verb',

            'stop': 'verb',

            'the': 'stop',

            'of': 'stop',

            'from':'stop',

            'at':'stop',

            'in':'stop',

            'Door':'Noun',

            'bear':'Noun',

            'princess':'Noun',

            'cabinet':'Noun',

            '123456789': 'number',

            } 

    def convert_numbers(s):

        

            try:

                return int(s)

            except ValueError:

                return s

    def scan(sentence):

        words = sentence.split()

        result = []

        for word in words:

            check_string = convert_numbers(word)

            if word in lexicon:

                check_number = convert_numbers(word)

                pair = (lexicon[word], check_number)

                result.append(pair)

            elif type(check_string) ==type(1):

                number = convert_numbers(word)

                if number:

                    pair = ('number', number)

                    result.append(pair)

            else:

                 error_word = word

                 pair = ('error', error_word)

                 result.append(pair)

                

        return result




I'm sorry for the  wrong code wrapping... Using this site on phone 😅

That’s weird. Sorry, I can’t make sense of this from what you’ve shown. test_directions looks fine, but maybe there’s something weird going on before the snippet that we can’t see here?

If you have the possibility to upload the whole project somewhere (e.g. GitHub) I’d be happy to try and see what’s going on.

1 Like

https://github.com/Merlin687/LPTHW-Ex48.git

This is the link to the Github repository of the project…It would be great help if you checked it out :slight_smile:

Okay, it was a simple one. In your test file you are missing a comma after ('direction', 'south'). Funnily, it’s correct in the snippet you posted earlier.

Is there a reason why you have the test files twice, once in the root directory and then in tests? And I see you’re writing for Python 2… any reason for not using 3? I think 2 is even considered deprecated now.

Last tip: next time, don’t upload zip files for collaboration. GitHub is awesome for browsing and collaborating on code when you just upload the project directory as is.

Thanks for the response …

When I used to run the test file individually directly from the terminal showed an error saying that the module ex48 doesn’t exist… That was rectified by moving it up…
My PC is a real old one and is literally running on fumes as a result its not compatible to Python 3😅

And lastly, I’m very new to github and wasn’t able to figure out how to upload the entire directory… I even installed git bash for the same but the effort didn’t yield anything so I finally went and did a zip file :sweat_smile:

Thnks for your consistent help.
:v:

Did you do the pip install -e thing to install your project?

As for GitHub: Did you see this?
https://docs.github.com/en/free-pro-team@latest/github/importing-your-projects-to-github/adding-an-existing-project-to-github-using-the-command-line

Yep, I was following this very article… But hit an error during the push origin command saying :

fatal : 'origin' does not appear to be a git repository
fatal : Could not read from remote repository.

Please make sure you have you have the correct access rights  
and the repository exists

In the root of your project, can you do git remote -v once and post the output?

Hey, I don’t think that will be necessary after you see this:

https://github.com/Merlin687/LPTHW-Ex48.git

I was making a STUPID mistake the first time which was causing the error, I corrected it and it all started working like a Well-Oiled Machine!!!

                               *Thanks!*
1 Like