2016-05-08 39 views
-2

我是python的新手,我一直在尝试从Head First Python运行此代码。 我目前的Python 2.7.9Nester模块在Python中不工作

跑这里是代码:

from __future__ import print_function 
import sys 
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: 
     print('the data file is missing') 

try: 
     with open('man_data.txt', 'w') as man_file: 
       print_lol(man, file = man_file) 
     with open('other_data.txt', 'w') as other_file: 
       print_lol(other, file = other_file) 

     man_file.close() 
     other_file.close() 

except IOError as err: 
     print('File error: ' + str(err)) 

这里是巢代码:

from __future__ import print_function 
import sys 
def print_lol(the_list, indent=False, level=0, fh=sys.stdout): 


for each_item in the_list: 
     if isinstance(each_item, list): 
      print_lol(each_item , indent, level+1, fh) 

     else: 
      if indent: 
        for tab_stop in range(level): 
         print("\t", end='', file=fh) 


      print(each_item, file=fh) 

但我不断收到此错误:

Traceback (most recent call last): 
    File "C:\Users\Olusegun\Desktop\Python codes\HeadFirstPython\Chapter 3\data.py", line 30, in <module> 
    print_lol(man, file = man_file) 
NameError: name 'print_lol' is not defined 

请帮助

+0

是很好......在每行的开头已经在它 – brw59

+1

代码你太前添加一个额外的4位远离学习,*请*切换到Python 3. Python 2是过去,Py3是语言的现在和未来。除非你有第一个学习2的非常具体的原因,否则使用3.你可以在以后总是找到差异。 Py2可以教会你一些会妨碍你进步的坏习惯。HFP也使用Py3,IIRC。 – MattDMo

回答

1

print_lolnester模块,您只需import nester,你应该叫print_lolnester.print_lol()

或者你可以from nester import print_lol,这样你可以调用print_lol

你应该知道,如果你格式化您的文章更好它将如何import Python modules

Python provides at least three different ways to import modules. You can use the import statement, the from statement, or the builtin import function. (There are more contrived ways to do this too, but that’s outside the scope for this small note.)

Anyway, here’s how these statements and functions work:

  • import X imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.

  • from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.

  • from X import a, b, c imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now use a and b and c in your program.

  • Finally, X = __import__(‘X’) works like import X, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.

+0

感谢亚历山大......那是有帮助的 – Chidexebere

+0

@Chidexebere,如果我的回答有帮助,请接受我的回答 –