Jump to content


PythonChen

Member Since 23 Apr 2014
Offline Last Active Apr 23 2014 01:48 PM
-----

Topics I've Started

Pythons Chapter4

23 April 2014 - 07:21 AM

Hello,
I'm on page 129, after I made the changes to nester.py and imported it onto sketch.py. I try to run it on idle shell, the following message keeps popping out.Traceback (most recent call last):
File "/Users/helloall/Documents/Python/sketch.py", line 29, in <module>
nester.print_lol(man, fh=man_file )
TypeError: print_lol() got an unexpected keyword argument 'fh'

This is my sketch.py code

import os
os.chdir('..')
os.chdir('headfirst_pratice')
import nester
os.chdir('..')
os.chdir('Python/chapter3')
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':')
line_spoken = line_spoken.strip()
if role == 'Man':
man.append(line_spoken)
elif role == 'Other Man' :
other.append(line_spoken)
else:
pass
except ValueError:
pass

data.close()
except IOError:
print('The Data file 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 )
nester.print_lol(other, other_file=fh)
except IOError as err:
print('File error: ' + str(err))

This is my nester.py
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)
Please help and thank you