Lesson 3


#1

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

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

i did a line break i may have done it wrong but I do not know how to do it right.



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

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


for b in my_list:
    my_file.write(str(b))
    my_file.write('/n')
my_file.close()



#2

you wrote the line break to its own line, you should write the line break on the same line as b variable. That is why you convert b to string, so you can concatenate b and new line


#3

like this?

my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
for b in my_list:
    my_file.write(str(b), '/n')
my_file.close()

#4

no, use + to concatenate b and new line character

using , is an argument separator. .write() is a method call, it should have one argument so , can’t be use to concatenate the string, given its separates arguments of method calls


#5

But this does not work either.

my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
for b in my_list:
    my_file.write(str(b)+'/n')
my_file.close()

#6

the append mode (r+) you use only works when the file exist, seems a bit of a bug. Use the write mode (w) instead


#7

this gives me an invalid syntax: File “python”, line 7
my_file.write(str(b)w+"/n")
^
SyntaxError: invalid syntax

‘’‘
my_list = [i**2 for i in range(1,11)]
my_file = open(“output.txt”, “r+”)
for b in my_list:
my_file.write(str(b)w+"/n")
my_file.close()
’’’


#8

the mode in which you open the file is determined here:

my_file = open(“output.txt”, “r+”)

not sure what you are doing


#9

you said this. What do you mean?


#10

when you open() the file, you select the mode in which you want to open the file. Try the writing mode (w) rather then the appending mode (r+)


#11

i had formated it the wrong way. i did the wrong slash id did / this instead of this \


#12

you did, good spot. I missed it


#13

thank you so much!!!


#14