Two Powershells showing running the scan function and running nosetests
In the updated learn python 3 the hard way, I am having trouble getting ex48 code to work. In this exercise we are given the test script (I've abbreviated the 6 cases down to 1 as they're all returning the same error) and told to write a lexicon.py file that makes the tests (through nosetests) return no errors:
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')])
And my code lexicon.py script is as follows:
directions = ('north', 'south', 'east')
verbs = ('go', 'kill', 'eat')
stops = ('the', 'in', 'of')
nouns = ('bear', 'princess')
def get_tuple(word):
lowercased = word.lower()
if lowercased in directions:
return('direction', lowercased)
elif lowercased in verbs:
return('verb', lowercased)
elif lowercased in stops:
return('stop', lowercased)
elif lowercased in nouns:
return('noun', lowercased)
elif lowercased.isdigit():
return('number', int(lowercased))
else:
return('error', word)
def scan(sentence):
pairs = []
text = str(sentence)
words = text.split()
for item in words:
try:
tup = get_tuple(item)
pairs.append(tup)
except:
pass
print(pairs)
Here is my error code:
File , line 6, in test_directions
assert_equal(lexicon.scan("north"), [('direction', 'north')])
AssertionError: None != [('direction', 'north')]
-------------------- >> begin captured stdout << ---------------------
[('direction', 'north')]
When I run the program in python directly in powershell, the scan function always seems to get the proper output. If I type in any string like "the north remembers", I do get the following output:
[('stop', 'the'), ('direction', 'north'), ('error', 'remembers)]
Can anyone see why nosetests throws this error?
EDIT - there was an issue with removing the 'return' command, but the underlying error is not return vs. print. Nosetests still has the same error with the following code:
I've now amended the code to look as follows:
def scan(sentence):
pairs = []
text = str(sentence)
words = text.split()
for item in words:
tup = get_tuple(item)
pairs.append(tup)
return pairs