Writing


#1


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

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')
my_file.close()

I can't Figure Out My Mistake!

Please Help!

Thanks!


#2

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)


#3

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

because you are iterating using item throught 'my_list'


#4

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