Having some trouble with Learn Python The Hard Way Excercise 48
In the section we are given a series of unit tests and need to create a function that will make the tests pass. Here is the test:
from nose.tools import *
from testing import *
def test_numbers():
assert_equal(scan("1234"), [('number', 1234)])
result = scan("3 91234")
assert_equal(result, [('number', 3),
('number', 91234)])
There are other aspects of the tests but this is the only one that isn't passing. Here is what I wrote:
def convert_number(s):
try:
return int(s)
except ValueError:
return None
def scan(s):
direction_words= ("north", "south", "east", "west", "down", "up", "left", "right", "back")
verbs= ("go", "stop", "kill", "eat")
stop_words= ("the", "in", "of", "from", "at", "it")
nouns= ("door", "bear", "princess", "cabinet")
numbers= s.split()
i=0
j=0
g=0
m=0
a=0
while a< len(numbers):
if type(convert_number(numbers[a])) == int:
return [('number', int(x) ) for x in s.split()]
else:
a += 1
while i < 9:
if direction_words[i] in s.split():
return [('direction', x ) for x in s.s开发者_开发技巧plit()]
else:
i+=1
while j < 4:
if verbs[j] in s.split():
return [('verb', x ) for x in s.split()]
else:
j+=1
while g < 6:
if stop_words[g] in s.split():
return [('stop', x ) for x in s.split()]
else:
g+=1
while m < 4:
if nouns[m] in s.split():
return [('noun', x ) for x in s.split()]
else:
m+=1
else:
return
Just add @staticmethod
above def scan(s):
to make it work.
I suspect that you certainly want def scan(self, s):
instead of def scan(s):
.
精彩评论