O'Reilly Forums: Ch. 4, Page 129 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ch. 4, Page 129 NameError: name 'print_lol' is not defined

#1 User is offline   njordan2001 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 05-February 12

Posted 19 February 2012 - 09:20 AM

So, I've edited the nester module. I then referred to chapter 2, and built my distribution and installed it to my local copy of python.

Here's my updated program:
import nester

man = []
other = []

try:
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError as oops:
    print('The datafile is missing! ' + str(oops))

try:
    with open('man_data.txt', 'w') as man_file, open('other_data.txt', 'w') as other_file:
        print_lol(man, fh=man_file)
        print_lol(other, fh=other_file)
        
except IOError as err:
    print('File error: ' + str(err))


But, I'm getting this 'print_lol' is not defined! I have a feeling that I'm just overlooking some small typo or missing one simple line of code...
0

#2 User is offline   njordan2001 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 05-February 12

Posted 19 February 2012 - 09:57 AM

Ok.... I decided to read a little further back in Chapter 2. Yep, found my problem.

I need to either specify that print_lol comes from the nester module:
nester.print_lol(man, fh=man_file)


Or, when I 'import nester', I can specify the 'print_lol' function there:
from nester import print_lol


The whole 'namespace' thing. This time I read it and took some time to think about it a little more and now understand and "agree" with the idea of using the 'nester.print_lol' way.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users