CrimsonEquinox (4) [Avatar] Offline
#1
Hello,

So up to this point I've been cruising pretty good, not really understanding everything but I have had moment's of "OH THAT'S HOW IT WORKS!". My problem is with listing 4.11. Till now my test_todo-1.py has been returning "OK" on all the functions, but now it's getting hung up in the test_todo_sort_order because it can't find "list_of_names" in todo.py.

names = [name.title() for name in list_of_names], NameError: name 'list_of_names' is not defined


Usually this is no big deal, I just pop open the source code and see what I am doing wrong and try to figure it out... this time however, listing 4.11 is no where in the source. I mean the "capitalize" function is there but nothing else.

What am I missing here?

Thanks,
CrimsonEquinox (4) [Avatar] Offline
#2
So, as far as I can tell..

def capitalize(todo):
todo['level'] = todo ['level'].upper()
return todo

is the ONLY part of 4.11 that is actually supposed to go into your code. Everything else is junk code, "examples" of how Line Comprehensions can be used. This language is hard enough to understand WITHOUT useless examples that go off topic. Other examples should be in their OWN sections, not jammed up in the code that you're trying to make work.

My head hurts now, this debug sucked. I tore through my code trying to figure out what I missed and what I wasn't doing right only to find out the code was not part of this program.


Later,

CE
boB Stepp (4) [Avatar] Offline
#3
I am about to start using this book myself. I jumped ahead to the point of your problem and what the author is trying to communicate seems to be quite clear. On the page prior to listing 4.11, he starts a new section, "List comprehensions". The last paragraph on that page (page 119) states:

"You can use a list comprehension to get exactly that and more. The following listing gives you a look at some common types of list comprehension and a feel for what they can do."

To my eye that says that what is to follow are examples to teach you some things about list comprehensions, NOT something that is meant to be part of the ongoing chapter program. Also, the title of listing 4.11 is "Lots of things you can do with list comprehensions". Again, obviously meant to be examples.

So, I feel you are being unfair to the author. This is a very common approach to presenting programming material: A mixture of short examples to illustrate a new concept and an ongoing chapter program to give you experience of seeing all of the chapter's ideas working in a complete programming example.

But I can understand your frustration in trying to work through a programming book alone! You might want to consider another resource, that is much more "lively" than this one: https://mail.python.org/mailman/listinfo/tutor

Python Tutor has a dedicated group of experts who are more than willing to help, but be sure to read and understand the instructions that you will receive once you subscribe to the list. No top posting, plain text emails only, clear subject line relevant to your question, statement of the Python version and operating system you are using, and a full copy/paste of the error messages you are getting. Also, be sure to copy and paste your relevant code fragment that is causing you trouble. If you try to type by hand you will inevitably make a mistake or screw up the indentation levels of your code.

I hope this might prove helpful!

Good luck!
boB Stepp