3. Writing


#1



I am stuck with this code. Can you please help me?


it looks like the file doesn't contain each element from my_list on its own line


why this is not working?


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("%s\n" % (item))
my_file.close()


#2

Hello :slight_smile:

Your code is correct. Please be more specific and explain why do you think this is not working? Is there any error message? What is inside the output.txt file?


#3

Without running lesson 2 first, this raised an exception in lesson 3. Must be we cannot create a file with "r+", only open for reading and writing if it exists. Use "w" instead. Passes for me.


#4

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