-1

Im starting to learn python can someone pls tell me how to OK this nosetest:

from nose.tools import *

    from lexicon.lexicon 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')])
    def test_numbers():
        assert_equal(lexicon.scan("1234"), [('number', 1234)])
        result = lexicon.scan("3 91234")
        assert_equal(result, [('number', 3),
                              ('number', 91234)])

I used the folowing code to pass the first test but couldnt pass test_numbers().

directions = ['north','south','east','east','down','up','left','right','back','front']
class lexicon:
    @staticmethod
    def scan(d):
        list1 = [('direction', x) for x in d.split() if x in directions]
        try:
            list2 = [('number',int(x)) for x in d.split() if int(x) in xrange(999999)]
        except ValueError:
            return None   
        return list1 + list2

I am thinking I am misusing the use of try .Please help me out

Bolaji
  • 556
  • 1
  • 6
  • 11

2 Answers2

0

This code:

try:
    x = something_that_raises_an_error()
except:
    return y

does not set x = y. It simply performs the statement inside the except block, so it returns y.

When you run scan("north") and it goes into the except ValueError: block it returns None. That means the function ends right there and scan("north") is None. Replace return None with either list2 = [] or return list1.

(also, if somehow your code set list2 = None that wouldn't work either because list1 + None will fail - you can't add a list to a non-list and None is not a list)

But this will only get you so far because now when you pass a mixture of words and numbers you won't get any of the numbers. You need to use the try block inside a loop so that it can do the right thing for each value in the list.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

This is a complete solution to the problem

directions =['north','south','east','east','down','up','left','right','back','front']
numbers = xrange(999999999)
class lexicon:
    @staticmethod
    def scan(d):
        list2=d.split()
        list1=[]
        list3=[]
        try:
            for x in d.split(): 

                if int(x) in xrange(999999999):
                    a = x

                    list1.append(a)
                    list2.remove(a)
                else:
                    print "yes"
        except:
            list99=[]
        for x in d.split():
            if x in directions:
                z2 = ("direction" , x)
                list3.append(z2)
            elif x in list1:
                z2 = ("number" , int(x))
                list3.append(z2)
            elif x in list2:
                z2 = ("error" , x)
                list3.append(z2)
        return list3

With this it runs perfect

Bolaji
  • 556
  • 1
  • 6
  • 11