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