1,p124, Error: Nameerror:name ' print_lol ' is not defined
To display the file, you need to change the call BIF Print () to the Print_lol () function in the Nester module of the second chapter, so you need to import the Nester module.
The first step is to modify the Print_lol () function in the second Chapter Nester module and update the module, update the method
The Print_lol () function is then called in sketch.py.
1 " "2 modification Time: 09193 What to modify: Change the call BIF Print () to the Print_lol () function in the Nester module of the second chapter, you need to import the Nester module4 " "5 ImportNester6 7man=[]8Other=[]9 Try:TenData=open ('Sketch.txt') One forEach_lineinchData: A Try: -(Role,line_spoken) =each_line.split (':', 1) -line_spoken=Line_spoken.strip () the ifrole=='Mans': - man.append (Line_spoken) - elifrole=='Other Mans': - other.append (Line_spoken) + exceptValueError: - Pass + data.close () A exceptIOError: at Print('The datafile is missing!') - - 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) in exceptIOError as err: - Print('File Error:'+STR (ERR))
This will cause an error:
Reason:
When a function of a module is called, the format is "module name. Function name ()"
So the code changes to:
1 " "2 modification Time: 09193 What to modify: Change the call BIF Print () to the Print_lol () function in the Nester module of the second chapter, you need to import the Nester module4 Note: When calling print_lol, you need to add the module name, Nester.print_lol () instead of calling Print_lol () directly.5 " "6 ImportNester7 8man=[]9Other=[]Ten Try: OneData=open ('Sketch.txt') A forEach_lineinchData: - Try: -(Role,line_spoken) =each_line.split (':', 1) theline_spoken=Line_spoken.strip () - ifrole=='Mans': - man.append (Line_spoken) - elifrole=='Other Mans': + other.append (Line_spoken) - exceptValueError: + Pass A data.close () at exceptIOError: - Print('The datafile is missing!') - - Try: -With open ('Man_data.txt','W') as Man_file,open ('Other_data.txt','W') as Other_file: -Nester.print_lol (man,fh=man_file) inNester.print_lol (other,fh=other_file) - exceptIOError as err: to Print('File Error:'+STR (ERR))
Head first Python Beginner's study notes (fourth chapter)