3. Writing


Hey, I'm having some issues with 3. Writing.
I think I know how it works, it's a pretty easy one.
However after 3 attemps I can't get the output right.
Output.txt looks like this:


and the code itself looks like this:

my_list = [i**2 for i in range(1,11)]

my_file = open("output.txt", "r+")

# Add your code below!
for i in my_list:
    my_file.write("%s\n" % (i))


I also tried

for i in my_list:
    my_file.write(str(i) + "\n")

But that gave me the exact same results.

And one more thing, I tried it about 3/4 times and I never changed line 3, but it now gives an error:

Traceback (most recent call last):
  File "python", line 3, in <module>
IOError: [Errno 2] No such file or directory: 'output.txt'

This error happens with both variations of the code. So now every time I make a change I don't know if my change isn't adequate, or this site or my browser is just bugging out a bit. Because I didn't change anything when that error started happening.
I can continue by the way. When I didn't get the error I couldn't complete it because output.txt was wrong.
When I got the error it ignored output.txt being wrong.


That looks like something that was there before you started writing, doesn't it? Is it meant to be there? How can you clear the file when you open it? https://docs.python.org/2/library/functions.html#open


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