Head first python cainiao Study Notes (chapter 4), python Study Notes

Source: Internet
Author: User

Head first python cainiao Study Notes (chapter 4), python Study Notes

1, p124, error: NameError: name 'print _ lol 'is not defined

To display in the file, you need to change the call of BIF print () to call the print_lol () function in the nester module in chapter 2. Therefore, you need to import the nester module.

First, modify the print_lol () function in Chapter 2 nester module and update the module.

Then call the print_lol () function in sketch. py.

1 ''' 2 modification time: 0919 3 Modification content: Change calling BIF print () to calling the print_lol () function in the nester module in Chapter 2, you need to import the nester Module 4 '''5 import nester 6 7 man = [] 8 other = [] 9 try: 10 data=open('sketch.txt ') 11 for each_line in data: 12 try: 13 (role, line_spoken) = each_line.split (':', 1) 14 line_spoken = line_spoken.strip () 15 if role = 'man': 16 Man. append (line_spoken) 17 elif role = 'otherman ': 18 Other. append (line_spoken) 19 failed t ValueError: 20 pass21 da Ta. close () 22 failed t IOError: 23 print (The datafile is missing! ') 24 25 try: 26 with open('man_data.txt', 'w') as man_file,open('other_data.txt ', 'w') as other_file: 27 print_lol (man, fh = man_file) 28 print_lol (other, fh = other_file) 29 bytes t IOError as err: 30 print ('file Error: '+ str (err ))

An error is reported:

Cause:

When calling a module function, the format is "module name. Function Name ()"

Therefore, the code is changed:

1 ''' 2 modification time: 0919 3 Modification content: Change calling BIF print () to calling the print_lol () function in the nester module in chapter 2. Import the nester Module 4. Note: when calling print_lol, add the module name, nester. print_lol () instead of directly calling print_lol (). 5 '''6 import nester 7 8 man = [] 9 other = [] 10 try: 11 dataworks open('sketch.txt ') 12 for each_line in data: 13 try: 14 (role, line_spoken) = each_line.split (':', 1) 15 line_spoken = line_spoken.strip () 16 if role = 'man': 17 Man. append (line_spoken) 18 elif role = 'other Man ': 19 other. append (line_spoken) 20 bytes t ValueError: 21 pass22 data. close () 23 bytes t IOError: 24 print ('the datafile is missing! ') 25 26 try: 27 with open('man_data.txt', 'w') as man_file,open('other_data.txt ', 'w') as other_file: 28 nester. print_lol (man, fh = man_file) 29 nester. print_lol (other, fh = other_file) 30 bytes t IOError as err: 31 print ('file Error: '+ str (err ))

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.