0

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
  • `scan` doesn't return anything, **it prints to the screen**, then *returns `None`*. – juanpa.arrivillaga Sep 20 '17 at 19:13
  • I vaguely remember trying return and not getting it to loop through each item in words, it would only do the first. Does this sound like a typical issue I'd run into if I just added a return into the for loop? – Matthew Janas Sep 20 '17 at 19:15
  • 1
    Yes, because then it will *always return on the first iteration*. You need to accumulate your results into some container in the loop, e.g. a `list`, then *return that container*. So, you are already *doing that*, except you simply `print(pairs)`, you need to `return pairs`. As an aside, don't use a bare `except` clause, what error are you expecting to catch in that `try-except`? – juanpa.arrivillaga Sep 20 '17 at 19:17
  • Thanks. I am trying to get the code to loop over more than just the first item but am stuck. I included new code above. It is becoming more clear how it isn't working, just not why yet – Matthew Janas Sep 20 '17 at 19:30
  • 1
    No, dude. Just replace `print(pairs)` with `return pairs` **in your original solution**. You don't need `print`. You should know that LPTHW is [*notorious*](https://sopython.com/wiki/LPTHW_Complaints) for bringing questions that reveal a fundamental misunderstanding due to the way it glosses over very important points, i.e. the *difference between `print` and `return`*. I don't hate Zed Shaw's approach, and think it can be quite good, but you should know, it can leave gaping holes in your knowledge. – juanpa.arrivillaga Sep 20 '17 at 19:33
  • 1
    Also, holy hell, why *are you `pop`ing from a list while iterating from it!*. Don't do that. Why would you do that? That will cause [bugs](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list) – juanpa.arrivillaga Sep 20 '17 at 19:35
  • So that does iterate over the whole list. I had that earlier and was convinced it was causing the errors but it was likely other errors I've incrementally fixed. Unforunately my nosetests is still throwing the exact same error with the updated code – Matthew Janas Sep 20 '17 at 19:41
  • I don't know how much more clear I can be. Your fundamental issue is understanding the difference between *printing to the screen* and `return`. Your original code would be correct if you simply change `print(pairs)` to `return pairs` **at the end of your function**. – juanpa.arrivillaga Sep 20 '17 at 19:42
  • Is there any other reason why nosetests would still throw the same error with 'return' replacing 'print'? I understand the difference between printing out and returning a list in this case and had already made that change above – Matthew Janas Sep 20 '17 at 19:48
  • Yes, you aren't saving your file, or you aren't running the test properly. There is *no way* for nosetest to be giving you the same error. First, you don't actually print anything to stdout, so there would be no captured standard out. – juanpa.arrivillaga Sep 20 '17 at 19:49
  • Image added. I definitely don't understand what is behind the 'stdout' and how that affects how I need to write my lexicon.py file to meet the test criteria – Matthew Janas Sep 20 '17 at 19:55
  • Dude, **ive tested your code and it passes the exact tests just fine**. You almost *certainly* haven't saved your `lexicon.py` file. You cannot tell the difference between `print` and `return` in a REPL, because it is a "Read-Evaluate-**Print**-Loop". What's behind that 'stdout' is that *you are printing to stdout* even if you don't think you are. – juanpa.arrivillaga Sep 20 '17 at 19:56
  • It's saved, I save it after every time and plenty of times after you suggested it to be safe. I'll go back to some other files that have passed nosetests and work with those for a bit to see what's different. – Matthew Janas Sep 20 '17 at 20:01
  • I don't know what to tell you. Try deleting any `__pycache__` folders. Like I said, I copy-pasted your code, and ran the tests, and they passed. The error message is *exactly what you would expect* if it were running your original code. Do me a favor,in powershell, do `type lexicon.py`, what does it output? – juanpa.arrivillaga Sep 20 '17 at 20:04
  • It was a directory error on my part. Nested directories with identical names and an old version of the same file. I appreciate the help (and I never doubted it worked on your end), just still at the stage where it's hard to discern both the why and how of errors at this point. Thanks again for the help – Matthew Janas Sep 20 '17 at 20:06

0 Answers0