Why name the tuple before calling the element?

I don’t understand why in the test file,in the “assert_equal” lines,how the computer knows what the ‘directions’ means when I type it before ‘north’.How does the computer know that the word before ‘north’ is not an element of a tuple as well?

from nose.tools import *
from 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_verbs():
    assert_equal(Lexicon.scan("go"), [('verb', 'go')])
    result = Lexicon.scan("go kill eat open")
    assert_equal(result, [('verb', 'go'),
                          ('verb', 'kill'),
                          ('verb', 'eat'), 
                          ('verb', 'open')])
def test_stops():
    assert_equal(Lexicon.scan("the"), [('stop', 'the')])
    result = Lexicon.scan("the in of")
    assert_equal(result, [('stop', 'the'),
                          ('stop', 'in'),
                          ('stop', 'of')])
def test_nouns():
    assert_equal(Lexicon.scan("bear"), [('noun', 'bear')])
    result = Lexicon.scan("bear princess")
    assert_equal(result, [('noun', 'bear'),
                          ('noun', 'princess')])
def test_numbers():
    assert_equal(Lexicon.scan("1234"), [('number', 1234)])
    result = Lexicon.scan("3 91234")
    assert_equal(result, [('number', 3),
                          ('number', 91234)])

def test_errors():
    assert_equal(Lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
    result = Lexicon.scan("bear IAS princess")
    assert_equal(result, [('noun', 'bear'),
                          ('error', 'IAS'),
                          ('noun', 'princess')])

def test_capitalization():
    result = Lexicon.scan("the The tHe thE")
    assert_equal(result, [('stop', 'the'),
                          ('stop', 'the'),
                          ('stop', 'the'),
                          ('stop', 'the')])

I’m not quite sure what you mean, but when you see the code:

('direction', 'north')

That is actually a tuple with two elements: ‘direction’ and ‘north’. It’s not naming anything.

I mean, why doesn’t assert_equal compare the first north to both direction and north in the second tuple. Why doesn’t it throw an error when it finds out that north is NOT equal to direction but is equal to north in the second tuple?

line 4: Scan takes in “north”, splits it then adds it to tuple (‘direction’, ‘north’) <— you have to write the code to make it do that. :wink:

1 Like

I think what you need to do is, instead of trying to describe your problem in English, you should write out sample code that does what you want, and then show the error you get compared to what you expect. For example, if you say:

assert_equal('north', ('direction', 'north'))

Then say:

“This gives me an error that north does not equal the direction/north but it should say they are equal.”

Then I can say, “No, because the first argument to assert_equal is a string, and the second is a tuple with two strings in it. Those are not equal. That’s like asking me why $100 is not equal to a box with two stacks of 200 $1 bills.”


What I don’t get is why I had to write direction in with the same parantheses as “north” and also display it as a string when it was in fact a tuple.Why can’t I display direction as the tuple and “north” being in the tuple like I’ve typed in above?Also thanks for the advice. I’ll ask questions in that manner in future problems.

Well, problem is that’s not really valid Python. I think you want it to be a certain way but Python is the king and gets to say what’s valid or not. It’s kind of like wishing cars drive on the inverse side of the road in your country. Say, in the US I drive on one side, and then if I go to the UK I have to adapt and drive on their side. I can’t just go, “Pfft. This place is stupid. I’m driving like I do in the US.” I’ll get into a car crash.

What you did here is like driving against traffic. So, to fix it you need to look at this line of code very very carefully:

assert_equal(Lexicon.scan("north"), [('direction', 'north')])

You’ve typed it out wrong nearly every time you’ve talked about it, so I’m guessing you don’t really understand it. First, look at the second parameter. That is a list. And what’s inside that? A tuple. And inside that? Two strings. If you give 3 words then it would be a list, with 3 tuples, each of those with 2 strings.

Now this tells me that you didn’t quite get the list exercise, so I’d say please review that. Next, you aren’t understanding what Lexicon.scan() has to return. So, what you should be doing is something like this:

return [('direction', 'north')]

Do that, make the test work, then you make your version of scan replicate this exactly. Then make it work for more than one word.

Try that.

Thanks, I really just wanted to know why I had to do that in that exact way.It’s just one of the rules I have to obey. And I typed it wrong on purpose in the second reply to show you the way I wanted to write it.I understood the syntax.Anyway, thank you for the help. I can now get on with my life and this excercise :slight_smile:

1 Like

Yes, it’s that way because Python says it’s that way. Not much you can do about. However, your way actually would be worse to understand for various reasons. It’d be interesting for you to get farther in Python and the come back to your suggestion to see how it wouldn’t work. Try that after you finish the book. Just come back here and take a look at what you wrote and see how your understanding of Python has changed.

You might be getting confused with what is a Tuple and what is not a Tuple, I know I did, and therefore unable to read the code correctly. Look at these examples:

These are NOT Tuples:


They are just a number, a string, and a variable in parenthesis.

These ARE Tuples:

(123, 456, 789)
("A", "B") 
(my_var, "abc", 123)

So what defines a Tuple? Is it the parenthesis? No. Is it that there is more than one element? No. It is the comma that defines it as a Tuple.

So to make my first example in to Tuples you would write it thus :


I hope this helps in some way :slight_smile:

1 Like