9/9 question


lines 1-7 is codec’s solution 9-14 are mine

on line 4-5 we see a “file” is that a bug? doesn’t it supposed to be “my_file?”
my code works on the editor but is that a good alternative? i just wrote it thinking maybe it’ll work to my suprise it did



You could also comment it out and get the same effect, so this isn’t a good test for whether it “works”

Lines 4-5 do not do anything sensible, no. And the way to figure that out is to know what each thing there refers to and what you’re doing with them (a greater degree of awareness/control)

As you already said, for starters, you have to refer to a particular file to do something with a file. And then additionally have a strong argument for why that would do what you want such as “this is a file object, here is an attribute that according to this here documentation is True when the file is closed” and perhaps also “and I have tested my understanding of this attribute by observing its result both when I know the file to be closed and when it’s open”


i don’t think that is the question, line 1 till 7 is codecademy’s solution (from get solution button). The question is if line 4 is correctly, shouldn’t it be if not my_file.closed?


If it’s codecademy who wrote that I’m even less surprised, some very poor understanding has been demonstrated in other “solutions” as well


yep, it is (i verified it). So, just to be absolutely clear, it should be my_file on line 4.

totally true


Well, no, it shouldn’t be there at all, it should read:

with open('text.txt', 'w') as my_file:
    my_file.write('My Data!')
    print my_file.closed

print my_file.closed

(also added another print to demonstrate that it is closed on exiting the with-statement, seemed to be missing the point without it >_<)
(obviously a silly thing to print out, so it should really just be the first two lines)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.