It Gives the following error message:
Oops, try again. It looks like your file doesn't contain each element from my_list on its own line.

I expected to print 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(i) + '\n')

I can't Figure Out My Mistake!

Please Help!



Are you sure that's the mode you want to open the file in? If the file doesn't exist then it will not be created. Previous file content will also be partially overwritten - normally you'd either want to overwrite everything or not at all.

Your code has no print statement in it. You should not expect it to print anything.

You aren't writing values from my_list to the file, consider what values the list has (you can print it) and consider which values you write to the file (you can print that as well)


Try this:
my_file.write(str(item)+ '\n')

because you are iterating using item throught 'my_list'


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