Working on a Head First Python book, 2010, I've encountered an exercise where I had to print a list into a specific file, and another list into another one. Did all the code, everything works, except for the print module which says the name of the file is not defined, which is pretty weird since the solution of the exercise it's the exact same code of mine.
import os
man = []
other = []
try:
data = open('ketchup.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!")
print(man)
print(other)
try:
out = open("man_speech.txt", "w")
out = open("other_speech.txt", "w")
print(man, file=man_speech) #HERE COMES THE ERROR
print(other, file=other_speech)
man_speech.close()
other_speech.close()
except IOError:
print("File error")
Here is the error from the IDLE:
Traceback (most recent call last):
File "C:\Users\Monok\Desktop\HeadFirstPython\chapter3\sketch2.py", line 34, in
print(man, file=man_speech)
NameError: name 'man_speech' is not defined
Am I doing something wrong about the syntax, or maybe I didn't get how the print module works? The book doesn't give me any clue about it. I've also checked for lot of questions here and in some other forums, but nothing seems wrong with my code, and I'm actually tilted.
No comments:
Post a Comment