3/9 Writing


#1



https://www.codecademy.com/courses/python-intermediate-en-OGNHh/0/3?curriculum_id=4f89dab3d788890003000096#

My code doesn't show up when i hit submit. It gives me the error message: Traceback (most recent call last):
File "python", line 3, in
IOError: [Errno 2] No such file or directory: 'output.txt'

It does say its correct though, and lets me pass on to the next lesson, so i'm not sure what i'm doing wrong...

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(str(i))
    my_file.write("/n")
    
my_file.close()


#2

@arraycoder32672,
Please read
https://discuss.codecademy.com/t/3-9-error/13699/2?u=leonhard.wettengmx.n
to understand read/write file's

And as solution, to get ownership of the output.txt file
https://discuss.codecademy.com/t/3-writing-why-doesnt-this-work/30987/4?u=leonhard.wettengmx.n

Creating your own file
https://discuss.codecademy.com/t/cannot-complete-lesson-file-not-found-error/42278/4?u=leonhard.wettengmx.n


#3

@arraycoder32672:

Firstly,delete all the contents from the output.txt file (select all and ctrl+backspace)

Then the code:
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(str(i)+"\n")
my_file.close()

You cannot have write statements on different lines in this case.


#4

First thing I notice is you have used ("/n") and not ("\n")


#5

try \ instead of / and also write it like (str(i) + "\n")


#6

Oops, try again. It looks like your file doesn't contain each element from my_list on its own line.

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

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

Add your code below!

for item in my_list:
my_file.write(str(item)+ '\n')
my_file.close()


#7

Alltough your code

is valid...
try using

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