3. Writing- code passes, but I get an error


#1

This is my code, which passes, but nothing gets printed and this is the error message I get:

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

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

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

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

#3

Try this

my_file.write(str(i) + "\n")


#4

Makes no difference- I get the same error


#5

I had that error, think it's some kind of bug. Type something inside output.txt and it should work.


#6

It seems to be a bug. Before you run the code, click on the output.txt tab and then on the first entry. Press return a few times. You can now run your code.

I guess this prompts the system into creating the output file ready for you to write to it


#7

this worked for me:
my_file.write(str(my_list[i])+ "\n")


#8

Try to change the range in the first line to 1 through 12:

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

and in your for loop:

my_file.write(str(i) + "\n")


#9

If you change the open to w and not r+, it works

my_file = open("output.txt", "w")


#10

I followed the instruction as is and works!!

Here is the code:
my_list = [i**2 for i in range(1,11)]

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

for c in my_list:

my_file.write(str(c)+"\n")

my_file.close()


#11

my_list= [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
it should be:

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

snd so is the below