My ex48 -

    lexicon = {
        'north': 'direction',
        'south': 'direction',
        'west': 'direction',
        'east' : 'direction',
        'go' : 'verb',
        'kill': 'verb',
        'eat': 'verb',
        'take': 'verb',
        'the': 'stop',
        'in': 'stop',
        'of': 'stop',
        'bear': 'noun',
        'princess': 'noun',

    def number_check(number):
            return True
        except ValueError:
            return False

    def scan(sentence):

        result = []
        sentence_lowercase = sentence.lower()
        words = sentence_lowercase.split()

        for word in words:
            word_type = lexicon.get(word)
            if word_type == None and number_check(word):
            elif word_type is not None:

        return result

So this is my solution for ex48. I had to do a work around for the final test . In the book :

It’s written as :

def test_errors():
[(‘error’, ‘ASDFADFASDF’)])

But I had to change the test to :

def test_errors():
[(‘error’, ‘asdfadfasdf’)])

Did I do this right ? My thinking is since all the input will be changed to lowercase anyway, there’s no way for the error tuple to be the original .


Instead of testing the whole tuple
Assert == (‘error’ , ‘some string’)

I’d just check for first tuple item.
Then you can add more tests with fuzzy data.

result = lexicon.scan(“random string”)
string_type = result[0]
#you can get the first item in a tuple like this result[0] and second like this result[1]
#then check if string_type == ‘error’
assert_equal(string_type, ‘error’)

If you decide to expand your test later, you can go through a list of data to check for ‘error’ words easier.

some_list = ['fuzzzy', 'spammy', 'aDEADparrot', 'CorporatePirates']

def test_errors():
    for item in some_list:
         result = lexicon.scan(item)
         string_type = result[0]
         assert_equal(string_type, 'error')


Interesting, you may have found a bug since it would be better to lowercase the result inside the scan. I’d say you solved it then.