Ex 48 - Confused

So we are coming down to the wire and I am actually finding myself understanding what’s going on. It’s a challenge for my thinking so thanks a heap.

I am just getting into Ex 48 or Lexicon as I call it.

Where I am confused. A lexicon is a list of allowable words right? Yet ex48 is all about the lexicon_test.py and making it work by adding to your lexicon.py. Do you understand where I am going with this.
We type the first few lines of the test, then create the lexicon to make it work.

Is Ex 48 about the test or about the lexicon?

It’s about both. Your test runs your code. Now, I’ve given you the test, you just have to write the code that makes the test pass. This is a really common thing for programmers to do since, as a programmer, you need to automate what you do and testing is something you do.

So, I recommend you watch the video on this one. I believe I go through the process of doing this, but if I don’t let me know and I’ll see about redoing the video to show exactly how it’s done.

But, try it first on your own just to see where you go. Think in terms of: write some of the test, then get just a tiny bit of your lexicon.py to work with the test, then keep doing that until the whole thing passes.

Zed, can you point me in the right direction please. Maybe I need to go back to a chapter.

It’s all well and good to type out what you have put on paper and copy what you put on video, but actually understanding it can be challenging. I suppose that is the same with any career path. Get someone to explain how to drive a car. To us it’s second nature, but there are some things you can’t explain.

I have managed to get the testing down pat and understand what every line is doing, except for the whole numbers and exceptions things. I could copy someone else’s code but it would not explain it to me.

From what I can understand, my lexicon_test.py file stays exactly how it is, but it is how the numbers are scanned in the lexicon.py file that I need changing. Right?

There are a multitude of examples on the web, but some people have turned your 37 line file into 70 lines.

Would I be safe to say it has to do with the scan(sentence): module. I am floundering…

Yes, numbers is pretty easy if you have your test working. The logic that works best is this:

try:
    number = int(word)
    # it's a number, if the code gets here, add it as a number tuple
except ValueError:
    # not a number, so see if it's in the words dict like you normally do

That’s it. When you attempt to convert something that’s not a number to a number you get a ValueError. That means you can use an except to handle that situation and then treat it like something that’s in a word.

Also, copying what someone else does is how everyone gets started. I can’t think of a single programmer ever who learned how to do something in the field totally on their own. There was always some paper, book, or other code that they copied to get started, then they changed it or wrote their own after they understood it. If you’re under the impression that you should be able to just crank out solutions without this kind of basic research, or that copying is wrong then you’re mistaken. Copying is awesome and you should do it a lot.

The trick though is to really try to understand what you copied after you get it working. What I like to do is change it in different ways to make sure I understand it, and then I like to try and make my own thing from memory, with a little peaking when I get stuck.

Edit: And yes, you should add to your test to test that numbers work.

2 Likes

Hie Zed

Where can i also watch this video?

Thanks

It’s a little late but describe me what you want to see and maybe I’ll do a live stream for it again. Reply here with what you want, and I’ll see what I can do.

A free service run by Zed A. Shaw for learncodethehardway.org.